#!/bin/sh
set -eu

HOOK_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
REPO_ROOT=$(CDPATH= cd -- "$HOOK_DIR/.." && pwd)
LOCAL_PYTHON="$REPO_ROOT/.venv/bin/python"
WINDOWS_PYTHON="$REPO_ROOT/.venv/Scripts/python.exe"

if [ -x "$LOCAL_PYTHON" ]; then
    PYTHON_BIN="$LOCAL_PYTHON"
elif [ -x "$WINDOWS_PYTHON" ]; then
    PYTHON_BIN="$WINDOWS_PYTHON"
elif command -v python3 >/dev/null 2>&1; then
    PYTHON_BIN=$(command -v python3)
else
    echo "python not available, cannot run pre-push validation"
    exit 1
fi

BRANCH=$(git rev-parse --abbrev-ref HEAD)

if [ "$BRANCH" = "main" ] && [ -z "${ALLOW_MAIN_PUSH:-}" ]; then
    echo "ERROR: direct push to main is blocked"
    echo "Use: git rel"
    echo "Emergency workaround: git push --no-verify"
    exit 1
fi

echo "▶ Running pre-commit hooks"
"$PYTHON_BIN" "$REPO_ROOT/scripts/run_repo_tool.py" pre-commit run --all-files

echo "▶ Running mypy"
"$PYTHON_BIN" "$REPO_ROOT/scripts/run_repo_tool.py" mypy .

echo "▶ Running test suite"
"$PYTHON_BIN" "$REPO_ROOT/scripts/run_repo_tool.py" pytest -q

echo "▶ Running release audit"
if [ -z "${SKIP_RELEASE_AUDIT:-}" ]; then
    exec "$REPO_ROOT/scripts/release_audit.sh"
fi

echo "ℹ Skipping release_audit (already executed)"
