#!/bin/bash

# Pre-commit hook for Amari: Enforce all tests pass before commits
# This is critical for a mathematical library to maintain correctness
# Optimized for crate-conditional testing to reduce commit time

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'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# ============================================================================
# Detect Changed Crates
# ============================================================================

# Get list of changed files in this commit
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR)

# Extract crate directories (first path component ending with /)
AFFECTED_CRATES=$(echo "$CHANGED_FILES" | grep -E '^(amari-[^/]+|amari)/' | cut -d'/' -f1 | sort -u)
CRATE_COUNT=$(echo "$AFFECTED_CRATES" | grep -v '^$' | wc -l)

# Check for workspace-level changes (Cargo.toml, .githooks, etc.)
WORKSPACE_CHANGES=$(echo "$CHANGED_FILES" | grep -E '^(Cargo\.toml|\.githooks|\.github|scripts)' || true)

# Check commit message for full-test override
COMMIT_MSG_FILE=".git/COMMIT_EDITMSG"
FORCE_FULL_TEST=false
if [ -f "$COMMIT_MSG_FILE" ]; then
    if grep -q '\[full-test\]' "$COMMIT_MSG_FILE" 2>/dev/null; then
        FORCE_FULL_TEST=true
    fi
fi

# Determine test strategy
RUN_FULL_TESTS=false
if [ "$FORCE_FULL_TEST" = true ]; then
    echo -e "${BLUE}🎯 Full test suite requested via [full-test] flag${NC}"
    RUN_FULL_TESTS=true
elif [ -n "$WORKSPACE_CHANGES" ]; then
    echo -e "${BLUE}🎯 Workspace-level changes detected, running full test suite${NC}"
    RUN_FULL_TESTS=true
elif [ "$CRATE_COUNT" -eq 0 ]; then
    echo -e "${BLUE}🎯 No crate changes detected, running full test suite${NC}"
    RUN_FULL_TESTS=true
elif [ "$CRATE_COUNT" -gt 1 ]; then
    echo -e "${BLUE}🎯 Multiple crates changed ($CRATE_COUNT), running full test suite${NC}"
    RUN_FULL_TESTS=true
else
    # Single crate changed
    CHANGED_CRATE=$(echo "$AFFECTED_CRATES" | head -1)
    echo -e "${GREEN}⚡ Single crate changed: $CHANGED_CRATE${NC}"
    echo -e "${GREEN}⚡ Running targeted tests (faster commits!)${NC}"
    echo -e "${BLUE}💡 Tip: Use [full-test] in commit message to force full suite${NC}"
    RUN_FULL_TESTS=false
fi

# ============================================================================
# Always Run: Fast Static Analysis
# ============================================================================

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}"

# ============================================================================
# Conditional Test Strategy
# ============================================================================

if [ "$RUN_FULL_TESTS" = true ]; then
    # ========================================================================
    # FULL TEST SUITE (workspace-wide)
    # ========================================================================

    echo -e "${YELLOW}🧪 Running full workspace test suite...${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 2>/dev/null; then
        echo -e "${YELLOW}⚠️ Tropical phantom type contracts not found (may be okay)${NC}"
    fi
    if ! cargo test --package amari-network verified_contracts --quiet 2>/dev/null; then
        echo -e "${YELLOW}⚠️ Network phantom type contracts not found (may be okay)${NC}"
    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}"

else
    # ========================================================================
    # TARGETED TEST SUITE (single crate)
    # ========================================================================

    echo -e "${YELLOW}🎯 Running targeted tests for $CHANGED_CRATE...${NC}"

    # Run all tests for the changed crate
    echo -e "${YELLOW}🧪 Testing $CHANGED_CRATE...${NC}"
    if ! cargo test --package "$CHANGED_CRATE" --quiet; then
        echo -e "${RED}❌ Tests failed for $CHANGED_CRATE!${NC}"
        echo "All crate tests must pass before committing"
        exit 1
    fi
    echo -e "${GREEN}✅ $CHANGED_CRATE tests passed${NC}"

    # Run doc tests for the changed crate
    echo -e "${YELLOW}📖 Testing $CHANGED_CRATE documentation...${NC}"
    if ! cargo test --package "$CHANGED_CRATE" --doc --quiet 2>/dev/null; then
        echo -e "${YELLOW}⚠️ No doc tests found for $CHANGED_CRATE (may be okay)${NC}"
    else
        echo -e "${GREEN}✅ $CHANGED_CRATE documentation tests passed${NC}"
    fi

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

    # Note about CI
    echo -e "${BLUE}ℹ️ Note: Full workspace test suite will run in CI/CD${NC}"
fi

# ============================================================================
# WASM Validation (always run, but only for relevant crates)
# ============================================================================

# Check if any WASM-relevant crates changed
WASM_CRATES=("amari" "amari-core" "amari-tropical" "amari-dual" "amari-network" "amari-relativistic" "amari-info-geom" "amari-enumerative" "amari-fusion" "amari-automata" "amari-wasm")
WASM_AFFECTED=false

if [ "$RUN_FULL_TESTS" = true ]; then
    WASM_AFFECTED=true
else
    for crate in "${WASM_CRATES[@]}"; do
        if [ "$CHANGED_CRATE" = "$crate" ]; then
            WASM_AFFECTED=true
            break
        fi
    done
fi

if [ "$WASM_AFFECTED" = true ]; then
    echo -e "${YELLOW}🌐 Validating WASM compatibility...${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

    if [ "$RUN_FULL_TESTS" = true ]; then
        # Full WASM validation
        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"
            exit 1
        fi

        echo -e "${YELLOW}  Validating individual crates for WASM compatibility...${NC}"
        for crate in "${WASM_CRATES[@]}"; do
            if [ "$crate" != "amari-wasm" ] && [ "$crate" != "amari" ]; then
                if ! cargo check --package "$crate" --target wasm32-unknown-unknown --quiet 2>/dev/null; then
                    echo -e "${YELLOW}⚠️ $crate not WASM-compatible (may be expected)${NC}"
                fi
            fi
        done
    else
        # Targeted WASM validation for changed crate
        if [ "$CHANGED_CRATE" = "amari-wasm" ]; then
            echo -e "${YELLOW}  Checking amari-wasm compilation...${NC}"
            if ! cargo check --package amari-wasm --target wasm32-unknown-unknown --quiet; then
                echo -e "${RED}❌ WASM compilation check failed!${NC}"
                exit 1
            fi
        elif [ "$CHANGED_CRATE" != "amari" ]; then
            echo -e "${YELLOW}  Checking $CHANGED_CRATE WASM compatibility...${NC}"
            if ! cargo check --package "$CHANGED_CRATE" --target wasm32-unknown-unknown --quiet 2>/dev/null; then
                echo -e "${YELLOW}⚠️ $CHANGED_CRATE not WASM-compatible (may be expected)${NC}"
            fi
        fi
    fi

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

# ============================================================================
# Success!
# ============================================================================

echo ""
echo -e "${GREEN}🎉 All pre-commit checks passed! Proceeding with commit...${NC}"
echo ""
if [ "$RUN_FULL_TESTS" = true ]; then
    echo "✨ Full mathematical correctness validated ✨"
    echo "All geometric algebra properties verified"
    echo "All tropical semiring axioms confirmed"
    echo "All phantom type constraints validated"
else
    echo "⚡ Targeted tests passed for $CHANGED_CRATE ⚡"
    echo "CI/CD will run full test suite"
    echo "💡 Use [full-test] in commit message to run full suite locally"
fi
echo ""
