#!/bin/bash
# Pre-push hook: Comprehensive quality checks (linting + formatting + docstrings + 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 --dev; 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

# Check docstring coverage (configured in pyproject.toml)
echo "→ Checking docstring coverage..."
if ! uv run python -m interrogate cuvis_ai_schemas/; then
    echo ""
    echo "✗ Docstring coverage check failed!"
    echo "  See configuration in pyproject.toml [tool.interrogate]"
    echo ""
    echo "  To see detailed missing docstrings:"
    echo "    interrogate -vv cuvis_ai_schemas/"
    echo ""
    echo "  To skip this check (not recommended):"
    echo "    git push --no-verify"
    echo ""
    exit 1
fi

# Run pytest with non-GPU tests
echo "→ Running pytest (excluding GPU tests)..."
if ! uv run python -m pytest tests/ -v --tb=line -m "not slow and not gpu"; then
    echo "✗ Tests failed. Please fix the failing tests and try again."
    exit 1
fi

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