#!/usr/bin/env bash
set -euo pipefail

if [ "${ARQERA_SKIP_PRECOMMIT:-}" = "1" ]; then
  echo "ARQERA pre-commit skipped." >&2
  exit 0
fi

python3 scripts/agent_output_verifier.py

staged_files=$(git diff --cached --name-only)
if [ -z "$staged_files" ]; then
  exit 0
fi

needs_log=0
while read -r file; do
  case "$file" in
    docs/agent_logs/*|docs/north_star_*|docs/AGENT_SUPERPASS_BRIEF.md|docs/north_star_master_map_*|docs/north_star_progress_*|docs/north_star_master_map_index.md|docs/north_star_master_map_governance.md|docs/arqera_factory_architecture_blueprint.md|docs/codex_agent_collaboration_protocol.md)
      ;;
    *)
      needs_log=1
      ;;
  esac
done <<< "$staged_files"

if [ "$needs_log" -eq 1 ]; then
  if ! grep -q '^docs/agent_logs/' <<< "$staged_files"; then
    echo "Pre-commit blocked: code changes require an agent log in docs/agent_logs/." >&2
    echo "Set ARQERA_SKIP_PRECOMMIT=1 to override." >&2
    exit 1
  fi
fi
