#!/bin/bash
# Pre-commit hook für Typewriter Trainer
# Führt Linting (ruff) und Tests vor jedem Commit aus.
#
# Installation:
#   cp .githooks/pre-commit .git/hooks/pre-commit
#   chmod +x .git/hooks/pre-commit

set -u

echo "🔎 Running pre-commit checks..."
echo ""

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 1. Linting mit ruff (falls installiert)
if python -m ruff --version >/dev/null 2>&1; then
    echo "🧹 Running ruff..."
    if ! python -m ruff check .; then
        echo -e "${RED}❌ Linting failed${NC}"
        echo "Bitte 'python -m ruff check .' beheben (ggf. '--fix')."
        exit 1
    fi
    echo -e "${GREEN}✅ Linting passed${NC}"
else
    echo -e "${YELLOW}⚠️  ruff nicht installiert – Linting übersprungen${NC}"
    echo "   Installation: pip install -r requirements-dev.txt"
fi

echo ""

# 2. Tests (Exit-Code-basiert, kein grep)
echo "🧪 Running tests..."
if ! python -m unittest discover -s tests -p "test_*.py"; then
    echo -e "${RED}❌ Tests failed${NC}"
    echo "Bitte die Tests vor dem Commit beheben."
    exit 1
fi
echo -e "${GREEN}✅ Tests passed${NC}"

echo ""
echo -e "${GREEN}✅ All checks passed! Proceeding with commit...${NC}"
echo ""
exit 0
