#!/usr/bin/env bash
# cm 工作流 · 任务标记双保险（灰度阶段启用，可选）
#
# 用途：防止 N5 漏标记——代码提交时校验 tasks.md 的勾选状态有同步更新。
# 安装（在代码仓库内）：
#   export CM_SPECS_DIR=/path/to/specs        # specs 文件夹路径（含 N.feature 目录）
#   cp templates/hooks/pre-commit-cm-task-check .git/hooks/pre-commit
#   chmod +x .git/hooks/pre-commit
#
# 逻辑：本次提交包含源码变更、且 CM_SPECS_DIR 已设置时，检查 specs 下
# 是否存在"最近修改时间晚于上次提交"的 tasks.md——没有则给出警告。
# 默认仅警告不阻断；置 CM_TASK_CHECK_STRICT=1 时阻断提交。
set -uo pipefail

[ -z "${CM_SPECS_DIR:-}" ] && exit 0            # 未启用则直接放行
[ -d "$CM_SPECS_DIR" ] || exit 0

find_python() {
  local candidate
  for candidate in python3 python; do
    if command -v "$candidate" >/dev/null 2>&1 &&
      "$candidate" -c 'import sys; raise SystemExit(sys.version_info < (3, 9))' >/dev/null 2>&1; then
      command -v "$candidate"
      return 0
    fi
  done
  return 1
}

# 本次提交是否包含源码变更（排除纯文档）
changed=$(git diff --cached --name-only | grep -vE '\.(md|txt)$' || true)
[ -z "$changed" ] && exit 0

last_commit_ts=$(git log -1 --format=%ct 2>/dev/null || echo 0)
python_bin="$(find_python 2>/dev/null || true)"
fresh_tasks=""
if [ -n "$python_bin" ]; then
  # BSD find on macOS has no -newermt. Python keeps this timestamp check
  # portable while preserving support for specs outside the code repository.
  fresh_tasks="$("$python_bin" - "$CM_SPECS_DIR" "$last_commit_ts" <<'PY'
from pathlib import Path
import sys

root = Path(sys.argv[1])
cutoff = float(sys.argv[2])
try:
    for path in root.rglob("tasks.md"):
        try:
            if path.is_file() and path.stat().st_mtime > cutoff:
                print(path)
        except OSError:
            continue
except OSError:
    pass
PY
  )"
fi

if [ -z "$fresh_tasks" ]; then
  echo "⚠ [cm] 本次提交含源码变更，但 $CM_SPECS_DIR 下没有新更新的 tasks.md。" >&2
  echo "  若刚完成某个任务，请确认 N5 已将其标记为 [x]（漏标会导致断点恢复时重复执行）。" >&2
  if [ "${CM_TASK_CHECK_STRICT:-0}" = "1" ]; then
    echo "  严格模式已开启：提交被阻断。确认无碍可用 git commit --no-verify 跳过。" >&2
    exit 1
  fi
fi
exit 0
