#!/usr/bin/env bash
# Pre-push gate: quick verification baseline (format + lint only).
# Full verification (mypy, render all, pytest) is expected to have already
# run via `devtools verify` before the push (see CONTRIBUTING.md).
# CI runs the complete baseline.
#
# When `devtools verify` completed successfully at the current HEAD, the
# stamp file at .cache/last-verify-head matches and this hook is a no-op.

set -euo pipefail

# Pre-flight: ensure checkout-local devtools is importable before attempting verification.
if ! python -m devtools --help >/dev/null 2>&1; then
    echo "pre-push: checkout-local devtools is not importable; enter the project devshell (nix develop) first." >&2
    exit 2
fi

STAMP_FILE=".cache/last-verify-head"

if [[ -f "$STAMP_FILE" ]]; then
    STAMPED=$(head -n1 "$STAMP_FILE")
    CURRENT=$(git rev-parse HEAD)
    if [[ "$STAMPED" == "$CURRENT" ]]; then
        echo "pre-push: HEAD already verified (${STAMPED:0:8}); skipping." >&2
        exit 0
    fi
    echo "pre-push: HEAD changed since last verify; running gate." >&2
fi

echo "pre-push: running quick verification baseline" >&2
python -m devtools verify --quick
