#!/bin/sh
# Aether Quant pre-commit gate. In order: no committed secret (`aq secrets-check`), the library gate, the formal
# inventory, every install hash-pinned, ruff on the staged .py files, and 100% coverage of the changed modules.
#
# Install once per clone: `aq hooks install` (a wrapper in .git/hooks, so the Git LFS hooks keep working).
# `aq check` runs this without committing; `aq hooks status` shows its state.
#
# Runs under git's POSIX sh (git-bash on Windows), so keep it portable.

set -e

repo_root=$(git rev-parse --show-toplevel)
cd "$repo_root"

# Prefer the installed `aq` console script; fall back to running the module
# directly with whichever Python is on PATH (venv-activated shells get theirs).
if command -v aq >/dev/null 2>&1; then
    aq secrets-check
elif command -v python >/dev/null 2>&1; then
    python aq_cli.py secrets-check
elif command -v python3 >/dev/null 2>&1; then
    python3 aq_cli.py secrets-check
else
    echo "pre-commit: could not find 'aq' or 'python' to run secrets-check" >&2
    exit 1
fi

# V6.6.4 - the library gate: a public function that no aether_quant facade exports blocks the commit (scripts/library_gate.py). It reads the source and imports nothing.
if command -v python >/dev/null 2>&1; then
    python scripts/library_gate.py
elif command -v python3 >/dev/null 2>&1; then
    python3 scripts/library_gate.py
fi

# V6.6.10 - the formal inventory: every public function of the trading and research paths is modelled in Lean, excluded with a
# reason, or counted in a backlog that only shrinks (scripts/formal_inventory.py; reads the source, imports nothing).
if command -v python >/dev/null 2>&1; then
    python scripts/formal_inventory.py
elif command -v python3 >/dev/null 2>&1; then
    python3 scripts/formal_inventory.py
fi

# V6.6.7 (#361) - every install hash-pinned, as the OpenSSF Scorecard reads it (scripts/scorecard_pins.py, about a second).
if command -v python >/dev/null 2>&1; then
    python scripts/scorecard_pins.py
elif command -v python3 >/dev/null 2>&1; then
    python3 scripts/scorecard_pins.py
fi

# ruff on the staged Python files (AGENTS.md: ruff clean before a commit), well under a second
staged=$(git diff --cached --name-only --diff-filter=ACMR -- '*.py')
if [ -n "$staged" ]; then
    if command -v ruff >/dev/null 2>&1; then
        ruff check $staged
    elif command -v python >/dev/null 2>&1; then
        python -m ruff check $staged
    fi
fi

# coverage stays at 100% after every commit (AGENTS.md "Coverage: 100% after every commit"): the tests that name each changed
# module run under coverage. A full `aq test --coverage` newer than the changes passes at once. Skip it only right after
# that full run: `aq commit --skip-coverage` (this commit) or `aq hooks coverage off [--once]` (a marker in .git/), both
# of which come down to the switch below; CI's python-tests still enforces 100%.
marker=$(git rev-parse --git-path aq-skip-coverage)
if [ "${AQ_SKIP_COVERAGE_HOOK:-0}" = "1" ]; then
    echo "pre-commit: coverage gate skipped (AQ_SKIP_COVERAGE_HOOK=1)"
elif [ -f "$marker" ]; then
    state=$(cat "$marker")
    echo "pre-commit: coverage gate skipped ($state; turn it on with: aq hooks coverage on)"
    if [ "$state" = "once" ]; then
        rm -f "$marker"
    fi
else
    start=$(date +%s)
    if command -v aq >/dev/null 2>&1; then
        aq test --coverage --changed
    elif command -v python >/dev/null 2>&1; then
        python aq_cli.py test --coverage --changed
    else
        python3 aq_cli.py test --coverage --changed
    fi
    echo "pre-commit: coverage of the changed modules checked in $(( $(date +%s) - start )) s"
fi
