#!/usr/bin/env bash
# View-conventions commit guard.
#
# Runs the shared rule engine (.claude/hooks/view-conventions-check.py, driven by
# .claude/conventions/views.json) against the STAGED version of changed *.php
# files and blocks the commit on any error-severity violation (use-braces,
# use-printAttrValue). Advisory rules (use-sieroty, …) are printed but never
# block. This is the single enforcement point for every commit, regardless of
# author or tool — the PostToolUse hook only sees Claude's own edits.
#
# Escape hatches:
#   - per line:  add a `// lint-skip:<rule-id>` (or `<!-- lint-skip:<rule-id> -->`)
#   - whole commit:  git commit --no-verify   (CI still enforces — see
#                    .github/workflows/view-conventions.yml)
#
# Exit codes from the checker: 0 = clean, 1 = violations (block), 2 = internal
# error (fail open — allow the commit but warn, so a checker bug never wedges
# the developer).

set -u

repo_root=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
checker="$repo_root/.claude/hooks/view-conventions-check.py"

# Nothing to enforce if the checker is missing (e.g. partial checkout).
[ -f "$checker" ] || exit 0

if ! command -v python3 >/dev/null 2>&1; then
  echo "view-conventions guard: python3 not found; skipping (commit allowed)." >&2
  exit 0
fi

python3 "$checker" --staged
rc=$?

if [ "$rc" -eq 1 ]; then
  echo "" >&2
  echo "✖ Commit blocked by the view-conventions guard (see above)." >&2
  echo "  Fix the violations, add a 'lint-skip:<rule>' directive, or bypass with:" >&2
  echo "      git commit --no-verify" >&2
  exit 1
fi

if [ "$rc" -gt 1 ]; then
  echo "view-conventions guard errored (rc=$rc); allowing commit. Please report this." >&2
fi

exit 0
