#!/bin/bash
# Desk2HA Pre-Commit Checks
# Runs lint, format check, and tests before allowing commits.
# Install: git config core.hooksPath .githooks

echo "Running pre-commit checks..."

# 1. Lint
echo "  [1/3] ruff check..."
python -m ruff check desk2ha_agent/ tests/ 2>/dev/null
if [ $? -ne 0 ]; then
    echo ""
    echo "COMMIT BLOCKED: Lint errors found. Fix with: python -m ruff check --fix desk2ha_agent/ tests/"
    exit 1
fi

# 2. Format
echo "  [2/3] ruff format --check..."
python -m ruff format --check desk2ha_agent/ tests/ 2>/dev/null
if [ $? -ne 0 ]; then
    echo ""
    echo "COMMIT BLOCKED: Formatting issues. Fix with: python -m ruff format desk2ha_agent/ tests/"
    exit 1
fi

# 3. Tests
echo "  [3/3] pytest..."
python -m pytest tests/ -q --tb=short 2>/dev/null
if [ $? -ne 0 ]; then
    echo ""
    echo "COMMIT BLOCKED: Tests failed. Fix the failing tests before committing."
    exit 1
fi

echo "All checks passed."
