# Pre-commit: enforce version bump, changelog update, and quality gate.
# Mirrors the CI quality steps (compile → lint → test) so failures are
# caught locally before push rather than discovered on the CI server.

set -e

# ── 1. Version bump + changelog enforcement ──────────────────────────────────

if git diff --cached --name-only | grep -qE 'src/|docs/|README.md|package.json|CONTRIBUTING.md|\.github/|wiki/'; then
  if ! git diff --cached --name-only | grep -q 'package.json'; then
    echo "ERROR: Every commit must include a version bump in package.json."
    exit 1
  fi
  if ! git diff --cached --name-only | grep -q 'CHANGELOG.md'; then
    echo "ERROR: Every commit must include a matching CHANGELOG.md entry."
    exit 1
  fi
fi

# ── 2. Quality gate (mirrors CI) ─────────────────────────────────────────────

echo "[pre-commit] Compiling TypeScript..."
npm run compile --silent

# ── 3. AI instruction sets are current ───────────────────────────────────────
#
# Verify only: this never edits a file, so the commit you staged is the commit
# that lands. It runs after the compile because it reads the built checker, and
# it exits 0 when it cannot run rather than blocking a fresh clone.
#
# Skip one commit with ATLASMIND_SKIP_INSTRUCTION_CHECK=1, or untick
# "Verify AI instruction sets before each commit" on Settings -> AI Instructions.

echo "[pre-commit] Checking AI instruction sets..."
node scripts/check-instruction-sync.mjs

echo "[pre-commit] Linting..."
npm run lint --silent

echo "[pre-commit] Running tests..."
npm run test --silent

echo "[pre-commit] All checks passed."
