#!/usr/bin/env bash
# ccsight pre-push hook.
#
# Re-runs the CI-blocking gate (cargo fmt --check, cargo test, cargo clippy,
# project lint) once before a push. pre-commit runs the same checks per
# `git commit`, but `git cherry-pick` does not fire it (nor does a commit
# made before the hooks were armed), so this is the last line that catches
# those before code leaves the machine. The advisory tools (shellcheck,
# cargo-audit) stay in pre-commit + CI.
#
# To install: `bash scripts/install-hooks.sh` (configures `core.hooksPath`).

set -e

# Locate repo root regardless of where git invokes the hook from.
REPO_ROOT="$(git rev-parse --show-toplevel)"
cd "$REPO_ROOT"

echo "[pre-push] cargo fmt -- --check ..."
if ! cargo fmt -- --check 2>&1; then
    echo "[pre-push] FAILED: formatter drift. Run 'cargo fmt' and amend/recommit before pushing."
    exit 1
fi

echo "[pre-push] cargo test ..."
if ! cargo test --quiet 2>&1; then
    echo "[pre-push] FAILED: cargo test reported failures."
    exit 1
fi

echo "[pre-push] cargo clippy -- -D warnings ..."
if ! cargo clippy --quiet -- -D warnings 2>&1; then
    echo "[pre-push] FAILED: clippy reported warnings/errors."
    exit 1
fi

echo "[pre-push] bash scripts/lint.sh ..."
if ! bash scripts/lint.sh; then
    echo "[pre-push] FAILED: project lint blocked the push."
    exit 1
fi

# Push is infrequent, so spend the time the per-commit gate can't: run the
# EXHAUSTIVE real-data aggregation-agreement check over ALL of `~/.claude`
# (the commit gate only samples it). No-ops in environments without local
# Claude data. Slow (minutes on a large history) by design.
echo "[pre-push] cargo test -- --ignored (full real-data agreement) ..."
if ! cargo test --quiet test_stats_and_grouper_agree_on_token_totals -- --ignored 2>&1; then
    echo "[pre-push] FAILED: full real-data aggregation paths diverged."
    exit 1
fi

echo "[pre-push] OK"
