#!/usr/bin/env bash
#
# Repartir Pre-Commit Hook
# Enforces quality gates before allowing commits
#
# This hook runs:
# - cargo clippy (zero warnings policy)
# - cargo fmt --check (formatting)
# - cargo test (fast unit tests)
# - mdbook build (book validation)
#
# Based on Iron Lotus Framework quality principles
# To install: ./scripts/install-hooks.sh
# To bypass (EMERGENCY ONLY): git commit --no-verify

set -e  # Exit on first error

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

echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}🔍 Repartir Quality Gates (Pre-Commit)${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"

# Run clippy
echo ""
echo -e "${BLUE}[1/5] Running clippy (zero warnings policy)...${NC}"
if ! cargo clippy --all-targets --all-features -- -D warnings 2>&1; then
    echo -e "${RED}❌ Clippy failed - commit blocked${NC}"
    echo ""
    echo "To fix:"
    echo "  1. Review clippy warnings above"
    echo "  2. Run: cargo clippy --fix --allow-dirty"
    echo ""
    echo "To bypass (NOT RECOMMENDED):"
    echo "  git commit --no-verify"
    exit 1
fi
echo -e "${GREEN}✅ Clippy: Zero warnings${NC}"

# Run formatting check
echo ""
echo -e "${BLUE}[2/5] Checking formatting...${NC}"
if ! cargo fmt --check; then
    echo -e "${RED}❌ Formatting check failed - commit blocked${NC}"
    echo ""
    echo "To fix:"
    echo "  cargo fmt"
    echo ""
    echo "To bypass (NOT RECOMMENDED):"
    echo "  git commit --no-verify"
    exit 1
fi
echo -e "${GREEN}✅ Formatting: Correct${NC}"

# Run fast tests
echo ""
echo -e "${BLUE}[3/5] Running tests...${NC}"
if ! cargo test --lib --quiet 2>&1 | tail -3; then
    echo -e "${RED}❌ Tests failed - commit blocked${NC}"
    echo ""
    echo "To fix:"
    echo "  1. Review test failures above"
    echo "  2. Run: cargo test"
    echo ""
    echo "To bypass (NOT RECOMMENDED):"
    echo "  git commit --no-verify"
    exit 1
fi
echo -e "${GREEN}✅ Tests: Passing${NC}"

# Validate book builds
echo ""
echo -e "${BLUE}[4/5] Building mdBook...${NC}"
if ! command -v mdbook &> /dev/null; then
    echo -e "${YELLOW}⚠️  mdbook not found - skipping book validation${NC}"
    echo "   Install mdbook: cargo install mdbook"
else
    if ! mdbook build book/ 2>&1 | grep -E "(error|ERROR)" ; then
        echo -e "${GREEN}✅ Book: Built successfully${NC}"
    else
        echo -e "${RED}❌ Book build failed - commit blocked${NC}"
        echo ""
        echo "To fix:"
        echo "  1. Review mdbook errors above"
        echo "  2. Fix broken links or invalid markdown"
        echo "  3. Run: mdbook build book/"
        echo ""
        echo "To bypass (NOT RECOMMENDED):"
        echo "  git commit --no-verify"
        exit 1
    fi
fi

# bashrs linting check (for bash scripts and Makefile)
echo ""
echo -e "${BLUE}[5/5] Running bashrs linting on staged files...${NC}"

# Check if bashrs is installed
if ! command -v bashrs &> /dev/null; then
    echo -e "${YELLOW}⚠️  bashrs not found - skipping shell linting${NC}"
    echo "   Install bashrs: cargo install bashrs"
else
    # Get staged bash/Makefile files
    STAGED_SHELL_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(sh|bash)$|^Makefile$' || true)

    if [ -n "$STAGED_SHELL_FILES" ]; then
        BASHRS_FAILED=0
        for file in $STAGED_SHELL_FILES; do
            if [ -f "$file" ]; then
                echo "   Linting: $file"
                if ! bashrs lint "$file" 2>&1 | grep -q "0 error(s)"; then
                    echo -e "${RED}❌ bashrs found errors in $file${NC}"
                    BASHRS_FAILED=1
                else
                    echo -e "   ✅ No issues"
                fi
            fi
        done

        if [ $BASHRS_FAILED -eq 1 ]; then
            echo -e "${RED}❌ bashrs linting failed - commit blocked${NC}"
            echo ""
            echo "To fix:"
            echo "  1. Review bashrs errors above"
            echo "  2. Fix shell script issues"
            echo ""
            echo "To bypass (NOT RECOMMENDED):"
            echo "  git commit --no-verify"
            exit 1
        fi

        echo -e "${GREEN}✅ bashrs: All scripts clean${NC}"
    else
        echo "   No bash/Makefile files staged"
    fi
fi

# All checks passed
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN}✅ All quality gates passed - proceeding with commit${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""

exit 0
