#!/usr/bin/env bash
# pre-push — run CI validation checks before allowing a push
#
# Install: cp deploy/hooks/pre-push .git/hooks/pre-push && chmod +x .git/hooks/pre-push
# .git/hooks/ is not tracked by git, so this tracked copy is the source of truth.
#
# Based on lesson #4 from docs/LESSONS.md:
#   ruff check && mypy finlet/ --ignore-missing-imports && pytest tests/ -v

set -euo pipefail

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

# Activate the virtualenv
if [ ! -f .venv/bin/activate ]; then
    echo "ERROR: .venv not found at $REPO_ROOT/.venv"
    echo "Create it with: python3 -m venv .venv && pip install -e '.[dev]'"
    exit 1
fi
source .venv/bin/activate

echo "=== Pre-push checks ==="
echo ""

# 1. Lint
echo "[1/3] ruff check finlet/ tests/"
if ! ruff check finlet/ tests/; then
    echo ""
    echo "FAILED: ruff found lint errors. Fix them before pushing."
    exit 1
fi
echo "PASSED"
echo ""

# 2. Type check
echo "[2/3] mypy finlet/ --ignore-missing-imports"
if ! mypy finlet/ --ignore-missing-imports; then
    echo ""
    echo "FAILED: mypy found type errors. Fix them before pushing."
    exit 1
fi
echo "PASSED"
echo ""

# 3. Tests
echo "[3/3] pytest tests/ -q --tb=short"
if ! pytest tests/ -q --tb=short; then
    echo ""
    echo "FAILED: tests did not pass. Fix them before pushing."
    exit 1
fi
echo "PASSED"
echo ""

echo "=== All pre-push checks passed ==="
