#!/bin/bash
# Pre-commit hook for scicode-lint
# Runs ruff (linting + formatting check) and mypy (type checking)

# Ensure locale is set (fixes "cannot change locale" errors in WSL/containers)
export LC_ALL=C.UTF-8 LANG=C.UTF-8 2>/dev/null || export LC_ALL=C LANG=C

echo "Running pre-commit checks..."

# Run ruff linting
echo "-> ruff check ."
if ! ruff check .; then
    echo ""
    echo "COMMIT BLOCKED: ruff check failed"
    echo "Fix: ruff check . --fix"
    exit 1
fi

# Run ruff format check (--check doesn't modify files, just checks)
echo "-> ruff format --check ."
if ! ruff format --check .; then
    echo ""
    echo "COMMIT BLOCKED: files need formatting"
    echo "Fix: ruff format ."
    exit 1
fi

# Run mypy type checking (patterns/ excluded via pyproject.toml)
echo "-> mypy ."
if ! mypy .; then
    echo ""
    echo "COMMIT BLOCKED: mypy type errors"
    echo "Fix the type errors above"
    exit 1
fi

echo "All pre-commit checks passed!"
