#!/bin/sh
# dacli pre-commit hook (P3, 2026-07-24) — 版本受控, 团队共用
#
# 激活: git config core.hooksPath scripts/githooks   (或跑 scripts/install-hooks.sh)
#
# 顺序:
#   1. 真质量门 scripts/quality-gate.sh (tsc+vitest+doc:check+validate-baseline+smoke) → FAIL 阻塞 commit
#   2. 仪式门 _gate_webapi (webapi 引用率报告) → 降级 advisory, 永不阻塞
#
# 病根修复 (audit 2026-07-23 §12.4): 之前唯一门是 _gate_webapi, 只查注释出处/引用率,
# 从不跑 tsc/vitest → 48 类型错误 + 20 失败单测能在"全绿"下合入 (假绿).
# 现在编译+单测是硬门, 出处报告只作参考.

ROOT="$(git rev-parse --show-toplevel)"

# 只在 dacli 仓库内跑 (在其他仓库提交时跳过)
if [ ! -f "$ROOT/scripts/quality-gate.sh" ]; then
  exit 0
fi

# === 1. 真质量门 (阻塞) ===
sh "$ROOT/scripts/quality-gate.sh"
GATE_EXIT=$?
if [ $GATE_EXIT -ne 0 ]; then
  echo ""
  echo "❌ dacli 质量门 FAIL (exit=$GATE_EXIT), commit 阻塞"
  echo "   修复 tsc/vitest 错误后重试, 或紧急绕过: DACLI_SKIP_GATE=1 git commit ..."
  exit 1
fi

# === 2. 仪式门 _gate_webapi (advisory, 不阻塞) ===
if [ -f "$ROOT/src/commands/_gate_webapi.ts" ]; then
  echo ""
  echo "ℹ️  webapi 镜像引用率报告 (advisory, 不阻塞):"
  npx tsx "$ROOT/src/commands/_gate_webapi.ts" 2>/dev/null || true
fi

exit 0
