#!/usr/bin/env bash
# Local commit gates (platform-gincommon's .githooks/pre-commit, ported): lock drift, formatting,
# lint, and the Enterprise Platform Observability Standard's fast checks (registry self-check,
# reference-rule metric names, metric-registry.md inventory drift), then the pre-commit
# framework's hooks from .pre-commit-config.yaml (mypy, pytest, whitespace). The promtool rule
# checks need Docker or promtool and run in CI (validate-quality.yml → make metrics-lint).
# Install with `make install-hooks` (also run by `make setup`). Skip once with --no-verify.
set -euo pipefail

# Another project's activated venv makes uv warn and can pick the wrong interpreter.
unset VIRTUAL_ENV

echo "[pre-commit] running lock-check..."
if ! make --no-print-directory lock-check; then
  echo "[pre-commit] uv.lock does not match pyproject.toml — run 'uv lock', review and stage it, then commit again" >&2
  exit 1
fi

echo "[pre-commit] running format-check..."
make --no-print-directory format-check

echo "[pre-commit] running lint..."
make --no-print-directory lint

echo "[pre-commit] checking observability registry and inventory..."
make --no-print-directory metrics-registry metrics-refs
if ! uv run fastapicommon-metricslint inventory | diff -q docs/observability/metric-registry.md - >/dev/null; then
  echo "[pre-commit] docs/observability/metric-registry.md is stale — run 'make metrics-inventory' and stage it" >&2
  exit 1
fi

echo "[pre-commit] running .pre-commit-config.yaml hooks (mypy, pytest, whitespace)..."
uv run pre-commit run --hook-stage pre-commit
