#!/usr/bin/env bash
# Local commit gates — the fast subset of CI's validate-quality gates, scoped to what is
# staged (no tests, no Docker): ruff format --check + ruff check on staged Python files,
# docs-check when the architecture docs are staged, lock-check when the dependency
# manifests are. Like the Go repo's hook it checks the working-tree copy of each staged
# file. Install with `make setup` or `make install-hooks`; bypass a single commit with
# `git commit --no-verify`. `make ci` remains the full local gate.
set -euo pipefail

py_files=()
docs_staged=0
deps_staged=0
while IFS= read -r -d '' f; do
  case "$f" in
    *.py) py_files+=("$f") ;;
  esac
  case "$f" in
    ARCHITECTURE.md | docs/architecture/*) docs_staged=1 ;;
    pyproject.toml | uv.lock) deps_staged=1 ;;
  esac
done < <(git diff --cached --name-only -z --diff-filter=ACMR)

if [ "${#py_files[@]}" -gt 0 ]; then
  echo "[pre-commit] ruff format --check (${#py_files[@]} staged Python file(s))..."
  if ! uv run ruff format --check -- "${py_files[@]}"; then
    echo "[pre-commit] not formatted — run 'make format', review, stage, and commit again."
    exit 1
  fi
  echo "[pre-commit] ruff check (staged Python files)..."
  uv run ruff check -- "${py_files[@]}"
fi

if [ "$docs_staged" = 1 ]; then
  echo "[pre-commit] running docs-check..."
  make --no-print-directory docs-check
fi

if [ "$deps_staged" = 1 ]; then
  echo "[pre-commit] running lock-check..."
  make --no-print-directory lock-check
fi
