#!/bin/bash
# Git pre-commit hook:
# 1. Enforce the device branch-prefix rule (.agents/rules/branch-naming.md)
#    via .githooks/branch-guard.sh.
# 2. Route staged governance documents to their dedicated checkers — each
#    document class has its own verifier (no all-in-one gate):
#      AGENTS.md, .agents/rules/*.md   -> bun run verify:agents-md
#      SKILL.md, skills references     -> bun run verify:skill-md
#    Local gates fail open when bun is unavailable; CI re-runs both.
source "$(dirname "$0")/branch-guard.sh"

guard_branch_name "$(git branch --show-current)" || exit 1

staged="$(git diff --cached --name-only --diff-filter=ACMR)"

if ! command -v bun >/dev/null 2>&1; then
  echo "⚠️  [pre-commit] bun 未找到，跳过治理文档校验（CI 会重新执行）。" >&2
  exit 0
fi

if printf '%s\n' "$staged" | grep -qx "AGENTS.md" || printf '%s\n' "$staged" | grep -q "^\.agents/rules/"; then
  bun run verify:agents-md || exit 1
fi

if printf '%s\n' "$staged" | grep -qE "^skills/seal-use/(SKILL\.md|references/[^/]+\.md)$"; then
  bun run verify:skill-md || exit 1
fi

exit 0
