#!/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}🔗 Running integration tests...${NC}"
if ! cargo test --workspace --tests --quiet; then
    echo -e "${RED}❌ Integration tests failed!${NC}"
    echo "Integration correctness must be maintained"
    exit 1
fi
echo -e "${GREEN}✅ Integration tests passed${NC}"

echo -e "${YELLOW}📖 Running documentation tests...${NC}"
if ! cargo test --workspace --doc --quiet; then
    echo -e "${RED}❌ Documentation tests failed!${NC}"
    echo "Documentation examples must be verified"
    exit 1
fi
echo -e "${GREEN}✅ Documentation tests passed${NC}"

echo -e "${YELLOW}📐 Verifying geometric algebra axioms...${NC}"
if ! cargo test --package amari-core geometric_axioms --quiet; then
    echo -e "${RED}❌ Geometric algebra axioms verification failed!${NC}"
    echo "Geometric algebra properties are fundamental to mathematical correctness"
    exit 1
fi
echo -e "${GREEN}✅ Geometric algebra axioms verified${NC}"

echo -e "${YELLOW}🏝️ Verifying tropical semiring axioms...${NC}"
if ! cargo test --package amari-tropical semiring_axioms --quiet; then
    echo -e "${RED}❌ Tropical semiring axioms verification failed!${NC}"
    echo "Tropical algebra properties are fundamental to mathematical correctness"
    exit 1
fi
echo -e "${GREEN}✅ Tropical semiring axioms verified${NC}"

echo -e "${YELLOW}👻 Validating phantom type safety...${NC}"
if ! cargo test --package amari-core verified_contracts --quiet; then
    echo -e "${RED}❌ Core phantom type safety validation failed!${NC}"
    echo "Type safety constraints are critical for mathematical correctness"
    exit 1
fi
if ! cargo test --package amari-tropical verified_contracts --quiet; then
    echo -e "${RED}❌ Tropical phantom type safety validation failed!${NC}"
    echo "Type safety constraints are critical for mathematical correctness"
    exit 1
fi
if ! cargo test --package amari-network verified_contracts --quiet; then
    echo -e "${RED}❌ Network phantom type safety validation failed!${NC}"
    echo "Type safety constraints are critical for mathematical correctness"
    exit 1
fi
echo -e "${GREEN}✅ Phantom type safety validated${NC}"

echo -e "${YELLOW}📊 Validating test coverage...${NC}"
TOTAL_TESTS=$(cargo test --workspace --lib --bins -- --list 2>/dev/null | grep -c "test " || echo "0")
if [ "$TOTAL_TESTS" -lt 300 ]; then
    echo -e "${RED}❌ Insufficient test coverage: $TOTAL_TESTS tests (minimum 300 required)${NC}"
    echo "Mathematical libraries require comprehensive test coverage"
    exit 1
fi
echo -e "${GREEN}✅ Test coverage validated: $TOTAL_TESTS tests${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 "${YELLOW}🌐 Validating WASM builds...${NC}"
# Ensure WASM target is installed
if ! rustup target list --installed | grep -q wasm32-unknown-unknown; then
    echo -e "${YELLOW}⚠️ Installing wasm32-unknown-unknown target...${NC}"
    rustup target add wasm32-unknown-unknown
fi

# Check WASM compilation with high-precision features
echo -e "${YELLOW}  Checking WASM compilation...${NC}"
if ! cargo check --package amari-wasm --target wasm32-unknown-unknown --quiet; then
    echo -e "${RED}❌ WASM compilation check failed!${NC}"
    echo "WASM build must succeed - rug dependency conflicts have been resolved"
    exit 1
fi

# Test individual crates against WASM target
echo -e "${YELLOW}  Validating individual crates for WASM compatibility...${NC}"
WASM_CRATES=("amari-core" "amari-tropical" "amari-dual" "amari-network" "amari-relativistic")
for crate in "${WASM_CRATES[@]}"; do
    if ! cargo check --package "$crate" --target wasm32-unknown-unknown --features high-precision --quiet; then
        echo -e "${RED}❌ $crate WASM compilation failed!${NC}"
        echo "Fix WASM compatibility issues in $crate before committing"
        exit 1
    fi
done

echo -e "${GREEN}✅ WASM validation completed${NC}"

echo -e "${YELLOW}🏗️ Validating individual crate builds...${NC}"
# Test each crate individually to catch crate-specific issues
CRATES=("amari-core" "amari-tropical" "amari-dual" "amari-enumerative" "amari-relativistic" "amari-info-geom" "amari-network" "amari-fusion" "amari-automata" "amari-gpu")

for crate in "${CRATES[@]}"; do
    echo -e "${YELLOW}  Testing $crate...${NC}"
    if ! cargo test --package "$crate" --quiet; then
        echo -e "${RED}❌ $crate tests failed!${NC}"
        echo "Fix $crate-specific issues before committing"
        exit 1
    fi
done

# Special handling for amari-wasm (WASM-specific testing)
echo -e "${YELLOW}  Testing amari-wasm...${NC}"
if ! cargo check --package amari-wasm --target wasm32-unknown-unknown --quiet; then
    echo -e "${RED}❌ amari-wasm compilation failed!${NC}"
    echo "Fix WASM compilation issues before committing"
    exit 1
fi

echo -e "${GREEN}✅ Individual crate builds validated${NC}"

echo -e "${YELLOW}🎯 Validating cross-compilation targets...${NC}"
# Ensure we can build for common deployment targets
if ! cargo check --target x86_64-unknown-linux-gnu --workspace --quiet; then
    echo -e "${RED}❌ Linux x86_64 build failed!${NC}"
    echo "Fix cross-compilation issues before committing"
    exit 1
fi

if ! cargo check --target wasm32-unknown-unknown --package amari-wasm --quiet; then
    echo -e "${RED}❌ WASM target build failed!${NC}"
    echo "Fix WASM target issues before committing"
    exit 1
fi

echo -e "${GREEN}✅ Cross-compilation targets validated${NC}"

echo -e "${GREEN}🎉 All pre-commit checks passed! Proceeding with commit...${NC}"
echo ""
echo "✨ Mathematical correctness maintained ✨"
echo "All geometric algebra properties verified"
echo "All tropical semiring axioms confirmed"
echo "All phantom type constraints validated"