#!/bin/bash
# Pre-push hook: Comprehensive quality checks (linting + formatting + tests)
# Skip with: git push --no-verify

set -e

# Unset GIT_DIR/GIT_WORK_TREE so that uv's internal git fetch calls
# (for git-based dependencies) don't inherit the hook's git context.
unset GIT_DIR GIT_WORK_TREE

echo "Running pre-push checks..."

# Sync dev dependencies first
echo "→ Syncing dev dependencies..."
if ! uv sync --all-extras; then
    echo "✗ Dev dependency sync failed. Please check your environment."
    exit 1
fi

# Run Ruff formatting first
echo "→ Running Ruff formatting..."
if ! uv run ruff format .; then
    echo "✗ Ruff formatting failed. Please fix the errors and try again."
    exit 1
fi

# Run Ruff linting with auto-fix (on formatted code)
echo "→ Running Ruff linting (with auto-fix)..."
if ! uv run ruff check . --fix; then
    echo "✗ Ruff linting failed. Please fix the errors and try again."
    exit 1
fi

# Run the same test subset as CI
echo "→ Running pytest (matching CI markers)..."
if ! uv run python -m pytest tests/ -v --tb=line -m "not slow and not gpu and not stress and not requires_data"; then
    echo "✗ Tests failed. Please fix the failing tests and try again."
    exit 1
fi

echo "✓ All pre-push checks passed!"
exit 0
