#!/usr/bin/env bash
#
# Pre-commit quality gate for the academic-workflow template.
#
# This closes the "a direct `git commit` bypasses every gate" hole: the
# full backtest suite + the quality score now run on EVERY commit, not
# only when you go through the skill.
#
# Install once per clone:   ./scripts/install-hooks.sh
# (sets core.hooksPath=.githooks so git finds this version-controlled hook.)
#
# It runs two gates against the STAGED changes and aborts the commit on
# failure:
#   1. Surface-sync (ALWAYS): count + enumerative-table-row consistency,
#      skill integrity, model-version currency — deterministic and fast.
#   2. Quality score (>= 80) on staged .qmd / .tex / .R files with rubrics.
#
# Escape hatches (use sparingly; record the reason in the commit message):
#   SKIP_QUALITY_GATE=1 git commit ...   # skip only the quality score
#   git commit --no-verify ...           # skip ALL hooks (last resort)
#
set -uo pipefail

REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" || {
    echo "pre-commit: not inside a git work tree" >&2
    exit 2
}
cd "$REPO_ROOT" || exit 2

# Validate the STAGED index, not the working tree, by stashing unstaged
# *tracked* changes — but ONLY when it is safe. If any single file has BOTH
# staged and unstaged changes (the `git add -p` workflow), a `stash --keep-index`
# + `pop` round-trip can conflict and write literal conflict markers into your
# file. In that case we skip the stash and validate the working tree as-is (with
# a warning) rather than risk corrupting your work. Untracked files are never
# stashed (avoids slow stashes of large data dirs). A failed pop is loud +
# recoverable, never silently swallowed.
_STASHED=0
# -z: without it git C-quotes a non-ASCII, quote or backslash name
# (".claude/hooks/verifica\303\247\303\243o.py"), and the battery trigger below never
# matched a staged hook spelt that way. Read NUL-delimited directly (not via
# `tr '\0' '\n'`) so paths containing embedded newlines stay intact.
_STAGED_ALL=()
_HOOK_STAGED=0
while IFS= read -r -d '' _f; do
    _STAGED_ALL+=("$_f")
    case "$_f" in
        .claude/hooks/*|.githooks/*|scripts/hook-battery.sh|scripts/file-issue.py)
            _HOOK_STAGED=1 ;;
        .claude/settings*.json)
            case "${_f#.claude/}" in */*) ;; *) _HOOK_STAGED=1 ;; esac ;;
    esac
done < <(git diff --cached --name-only -z 2>/dev/null)

_UNSTAGED_ALL=()
_overlap=0
while IFS= read -r -d '' _u; do
    _UNSTAGED_ALL+=("$_u")
    for _s in ${_STAGED_ALL[@]+"${_STAGED_ALL[@]}"}; do
        if [ "$_u" = "$_s" ]; then
            _overlap=$((_overlap + 1))
            break
        fi
    done
done < <(git diff --name-only -z 2>/dev/null)
# Two tracked paths that differ only by case or Unicode form are one file on a macOS
# or Windows disk: the tree is dirty for good, the pop below always failed, and every
# commit left one more "pre-commit-gate" stash behind (#171). Same rule as
# scripts/check-repo-hygiene.py (NFC + casefold, each path prefix), which names the
# pair and fails the commit. NUL-delimited: without -z git C-quotes an accented name.
_collide=""
if [ "${#_UNSTAGED_ALL[@]}" -gt 0 ]; then
    _collide="$(git ls-files -z 2>/dev/null | python3 -c '
import sys, unicodedata
seen = {}
for f in sys.stdin.buffer.read().decode("utf-8", "surrogateescape").split("\0"):
    parts = f.split("/") if f else []
    for i in range(1, len(parts) + 1):
        p = "/".join(parts[:i])
        first = seen.setdefault(unicodedata.normalize("NFC", p).casefold(), p)
        if first != p:
            sys.stdout.buffer.write((first + " and " + p).encode("utf-8", "surrogateescape"))
            sys.exit(0)
' 2>/dev/null || true)"
fi
if [ "${_overlap:-0}" -gt 0 ] 2>/dev/null; then
    echo "ℹ pre-commit: ${_overlap} file(s) have both staged and unstaged changes —" >&2
    echo "  validating the WORKING TREE (not the pure staged index) to avoid a stash conflict." >&2
elif [ -n "$_collide" ]; then
    echo "ℹ pre-commit: tracked paths $_collide differ only by case or Unicode form, so the" >&2
    echo "  working tree cannot be stashed clean — validating the WORKING TREE (not the pure staged index)." >&2
elif [ "${#_UNSTAGED_ALL[@]}" -gt 0 ]; then
    # A stash is ours only if refs/stash moved. `stash push` exits 0 having made none
    # when nothing is staged (`commit --amend`) and the only unstaged change is one it
    # ignores (a submodule's own dirty content): the trap then popped the user's own
    # newest stash into the tree (#171).
    _before="$(git rev-parse -q --verify refs/stash 2>/dev/null || true)"
    if git stash push --keep-index --quiet --message "pre-commit-gate" 2>/dev/null \
            && [ "$(git rev-parse -q --verify refs/stash 2>/dev/null || true)" != "$_before" ]; then
        _STASHED=1
        trap 'if [ "$_STASHED" = "1" ]; then git stash pop --quiet 2>/dev/null || echo "⚠ pre-commit: could not auto-restore unstaged changes — recover with: git stash pop" >&2; fi' EXIT
        # A path can stay modified with nothing left to stash: a CRLF blob under a later
        # `eol=lf` rule, a clean filter that rewrites it. The pop then always failed, the
        # commit went through, and each one moved the user's unstaged work into another
        # "pre-commit-gate" stash (#171). Put the work back now and drop the stash:
        # every path whose working copy differs from the stash's (a still-dirty path
        # the user also edited included), --worktree only so nothing is staged.
        _still=""
        IFS= read -r -d '' _still < <(git diff --name-only -z --no-ext-diff --ignore-submodules 2>/dev/null) || true
        if [ -n "$_still" ]; then
            if git diff --quiet --no-ext-diff --ignore-submodules 'stash@{0}' 2>/dev/null \
                    || git diff --name-only -z --no-ext-diff --ignore-submodules 'stash@{0}' 2>/dev/null \
                        | git --literal-pathspecs restore --source='stash@{0}' --worktree \
                              --pathspec-from-file=- --pathspec-file-nul 2>/dev/null; then
                git stash drop --quiet 'stash@{0}' 2>/dev/null
                _STASHED=0
                echo "ℹ pre-commit: '$_still' stays modified after stashing (a line-ending or filter rule its" >&2
                echo "  committed bytes predate?) — validating the WORKING TREE (not the pure staged index)." >&2
            fi
        fi
    fi
fi

FAIL=0

# The hook battery re-fires every guard hook and is most of the suite's run time.
# Its result can only change when a hook, the settings that wire them, this git
# hook, the battery itself or a script it runs (scripts/file-issue.py, section i)
# changes — so skip it here when none of those is staged. A portability case
# (test_gitout.py) stages every $ROOT/ path the battery runs and fails if one is
# missing from the list above. CI runs the full suite on every push; nothing merges unbatteried.
if [ "$_HOOK_STAGED" = "1" ]; then
    unset BACKTEST_SKIP_HOOK_BATTERY      # a hook is staged: run it, whatever the shell inherited
else
    export BACKTEST_SKIP_HOOK_BATTERY=1
    echo "ℹ pre-commit: no hook, hook-settings or battery change staged — the hook battery is skipped here (CI runs it)."
fi

echo "── pre-commit: backtest (gate suite — see scripts/backtest.sh) ──"
./scripts/backtest.sh
[ $? -ne 0 ] && FAIL=1

if [ "${SKIP_QUALITY_GATE:-0}" = "1" ]; then
    echo ""
    echo "── pre-commit: quality gate SKIPPED (SKIP_QUALITY_GATE=1) ──"
else
    # Staged files that have quality rubrics: added, copied, modified, RENAMED or
    # type-changed. `ACM` alone dropped a file renamed and edited in one commit (git
    # reports it as R), so a syntax-broken script committed unscored. Read
    # NUL-delimited: without -z git C-quotes a non-ASCII, quote or backslash name
    # ("Slides/Aula_introdu\303\247\303\243o.tex"), the suffix match failed on the
    # closing quote, and the file was never scored.
    STAGED=()
    while IFS= read -r -d '' f; do
        case "$f" in *.[qQ][mM][dD]|*.[tT][eE][xX]|*.[rR]) STAGED+=("$f") ;; esac
    done < <(git diff --cached --name-only -z --diff-filter=ACMRT)
    if [ "${#STAGED[@]}" -gt 0 ]; then
        echo ""
        echo "── pre-commit: quality score (>= 80) on ${#STAGED[@]} staged file(s) ──"
        # Score each file individually: portable (no mapfile/bash4), handles
        # any byte in a path, and gives a per-file exit code we can classify.
        for f in "${STAGED[@]}"; do
            if [ ! -f "$f" ]; then
                # Staged, then deleted from the working tree: the commit carries a
                # file this gate cannot read. Skipping it here was a silent pass.
                # A dangling symlink lands here too, and it IS in the working tree:
                # name it as what it is, or the user goes looking for a deleted file.
                if [ -L "$f" ]; then
                    echo "✗ quality gate: '$f' is a symlink whose target is missing or not a file, so it cannot be scored — fix the link or unstage it." >&2
                else
                    echo "✗ quality gate: '$f' is staged but missing from the working tree, so it cannot be scored — restore it or unstage it." >&2
                fi
                FAIL=1
                continue
            fi
            python3 scripts/quality_score.py -- "$f"
            rc=$?
            # quality_score.py: rc 2 = auto-fail (compile/syntax FAILURE), rc 1 =
            # score < 80 (or scorer error / file-not-found). BOTH must block — a
            # file that fails to compile is the worst case, not an "infra error".
            if [ "$rc" -ne 0 ]; then
                echo "✗ quality gate: '$f' failed (rc=$rc — 2=compile/syntax failure, 1=score<80 or scorer error)." >&2
                FAIL=1
            fi
        done
    else
        echo ""
        echo "── pre-commit: no staged .qmd/.tex/.R files to score ──"
    fi
fi

if [ "$FAIL" -ne 0 ]; then
    echo "" >&2
    echo "✗ pre-commit gate failed — commit aborted." >&2
    echo "  Fix the findings above, or (sparingly) bypass with SKIP_QUALITY_GATE=1 / --no-verify." >&2
    exit 1
fi

echo ""
echo "✓ pre-commit gates passed."
exit 0
