#!/usr/bin/env sh
# husky pre-commit hook for CloudBase-MCP
set -e

echo "[husky] Running pre-commit checks..."

# Ensure dependencies are installed (webpack etc.)
if [ ! -d "mcp/node_modules" ]; then
  echo "[husky] Installing mcp/ dependencies (webpack, typescript, etc.)..."
  (cd mcp && npm install --no-audit --no-fund)
fi

# --- Step 1: i18n coverage ratchet (mcp/src 参数级 .describe 中文只减不增) ---
# 只在 mcp i18n 面有改动时跑，避免无关提交被拖慢（脚本零依赖、毫秒级完成）。
# 与 .github/workflows/i18n-guard.yml 同一道闸（CI 侧），逻辑在
# mcp/scripts/check-i18n-coverage.mjs。注意本文件只在上游 dev 的机器上生效：
# 若 core.hooksPath 被全局指向了别的目录（如 lefthook 的 ~/.git-hooks），
# 本钩子会被整体绕过，此时由 lefthook.yml 的同名 job 接手。
if git diff --cached --name-only | grep -qE '^mcp/(src/|scripts/check-i18n-coverage\.mjs|i18n-coverage-baseline\.json)'; then
  echo "[husky] mcp i18n surface changed, running i18n coverage check..."
  node mcp/scripts/check-i18n-coverage.mjs
fi

# --- Step 2: Sync mirrors if config/source/** was modified ---
if git diff --cached --name-only | grep -q "^config/source/"; then
  echo "[husky] config/source/ modified, running sync:config..."
  node scripts/sync-config.mjs

  # Stage any mirror files updated by the sync
  for dir in \
    config/.claude/skills/ config/.cursor/rules/ config/.github-copilot/rules/ \
    config/.windsurf/rules/ config/.codebuddy/rules/ config/.augment/rules/ \
    config/.aider/rules/ config/.opensumi/rules/ config/.kiro/steering/ \
    config/.qoder/rules/ config/.trae/rules/; do
    if [ -d "$dir" ]; then
      git add "$dir" 2>/dev/null || true
    fi
  done

  for dir in \
    .agent/rules/ .cline/rules/ .cursor/rules/ .github-copilot/rules/ \
    .windsurf/rules/ .codebuddy/skills/ .augment/guidelines/ \
    .opensumi/rules/ .kiro/steering/ .qoder/rules/ .trae/rules/; do
    if [ -d "$dir" ]; then
      git add "$dir" 2>/dev/null || true
    fi
  done

  if ! git diff --cached --quiet; then
    echo "[husky] Mirror files updated and staged."
  fi
else
  echo "[husky] Skipping sync:config (config/source/ not modified)."
fi

# --- Step 3: Build check (MUST pass) ---
echo "[husky] Running npm run build..."
npm run build
if [ $? -ne 0 ]; then
  echo "[husky] ERROR: npm run build failed. Fix errors before committing."
  exit 1
fi

echo "[husky] All checks passed."
