#!/bin/bash

# Pre-commit hook for Amari: Enforce all tests pass before commits
# This is critical for a mathematical library to maintain correctness

set -e  # Exit on any command failure

echo "🧪 Running Amari pre-commit checks..."

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}📏 Checking code formatting...${NC}"
if ! cargo fmt --check; then
    echo -e "${RED}❌ Code formatting check failed!${NC}"
    echo "Run 'cargo fmt' to fix formatting issues"
    exit 1
fi
echo -e "${GREEN}✅ Code formatting is correct${NC}"

echo -e "${YELLOW}🔍 Running Clippy lints...${NC}"
if ! cargo clippy --workspace --all-targets --all-features -- -D warnings; then
    echo -e "${RED}❌ Clippy found issues!${NC}"
    echo "Fix the warnings above before committing"
    exit 1
fi
echo -e "${GREEN}✅ Clippy checks passed${NC}"

echo -e "${YELLOW}🧪 Running core mathematical tests...${NC}"
if ! cargo test --workspace --quiet; then
    echo -e "${RED}❌ Core tests failed!${NC}"
    echo "All tests must pass before committing to maintain mathematical correctness"
    exit 1
fi
echo -e "${GREEN}✅ Core tests passed${NC}"

echo -e "${YELLOW}🔬 Running formal verification tests...${NC}"
if ! cargo test --workspace --features formal-verification --quiet; then
    echo -e "${RED}❌ Formal verification tests failed!${NC}"
    echo "Formal verification is critical for mathematical correctness"
    exit 1
fi
echo -e "${GREEN}✅ Formal verification tests passed${NC}"

echo -e "${YELLOW}📚 Checking documentation builds...${NC}"
if ! cargo doc --workspace --no-deps --quiet; then
    echo -e "${RED}❌ Documentation build failed!${NC}"
    echo "Fix documentation issues before committing"
    exit 1
fi
echo -e "${GREEN}✅ Documentation builds successfully${NC}"

echo -e "${GREEN}🎉 All pre-commit checks passed! Proceeding with commit...${NC}"
echo ""
echo "✨ Mathematical correctness maintained ✨"