#!/usr/bin/env bash
# Pre-commit: run lint, format check, type check, and tests.
# Bypass with: git commit --no-verify

set -e

echo "Running pre-commit checks..."

echo "  Lint..."
uv run ruff check src/ tests/

echo "  Format..."
uv run ruff format --check src/ tests/

echo "  Type check..."
uv run mypy src/

echo "  Version bump check..."
# If source files changed, version must be bumped too
src_changed=$(git diff --cached --name-only -- 'src/' | grep -v '__pycache__' | head -1)
if [ -n "$src_changed" ]; then
    version_changed=$(git diff --cached --name-only -- 'pyproject.toml' 'src/java_functional_lsp/__init__.py' | head -1)
    if [ -z "$version_changed" ]; then
        echo "ERROR: Source files changed but version was not bumped."
        echo "Update the version in both pyproject.toml and src/java_functional_lsp/__init__.py"
        echo ""
        echo "To bypass (docs/tests-only changes): git commit --no-verify"
        exit 1
    fi
fi

echo "  Tests..."
uv run pytest -q

echo "All checks passed."
