#!/usr/bin/env bash
#
# PMAT TDG Enforcement Pre-Commit Hook
# Auto-generated by `pmat hooks install --tdg-enforcement`
# DO NOT EDIT MANUALLY - Regenerate with `pmat hooks refresh`
#
# This hook enforces quality gates before allowing commits:
# - Checks for quality regressions against baseline
# - Enforces minimum quality grades for new/modified files
# - Blocks commits that violate configured thresholds
#
# Configuration: .pmat/tdg-rules.toml

set -e  # Exit on first error

# ZERO BRANCHING ENFORCEMENT (runs FIRST, before all other checks)
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
if [ -x "${SCRIPT_DIR}/pre-commit-branch-enforcer" ]; then
    "${SCRIPT_DIR}/pre-commit-branch-enforcer"
fi

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

# Configuration (injected by pmat hooks install)
BASELINE_PATH="{{BASELINE_PATH}}"
MIN_GRADE="{{MIN_GRADE}}"
MAX_SCORE_DROP="{{MAX_SCORE_DROP}}"
ALLOW_GRADE_DROP="{{ALLOW_GRADE_DROP}}"
MODE="{{MODE}}"
BLOCK_ON_REGRESSION="{{BLOCK_ON_REGRESSION}}"
BLOCK_ON_NEW_FILES="{{BLOCK_ON_NEW_FILES}}"

echo -e "${BLUE}🔍 PMAT TDG Quality Enforcement${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Check if TDG enforcement is disabled
if [ "${MODE}" = "disabled" ]; then
    echo -e "${YELLOW}⚠️  TDG enforcement disabled in config${NC}"
    exit 0
fi

# Check if pmat binary is available
if ! command -v pmat &> /dev/null; then
    echo -e "${RED}❌ Error: pmat binary not found in PATH${NC}"
    echo "   Install pmat: cargo install pmat"
    exit 1
fi

# Check if baseline exists
if [ ! -f "${BASELINE_PATH}" ]; then
    echo -e "${YELLOW}⚠️  No baseline found at ${BASELINE_PATH}${NC}"
    echo "   Creating initial baseline..."
    pmat tdg baseline create --output "${BASELINE_PATH}" --path .

    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✅ Initial baseline created${NC}"
        echo "   Future commits will be checked against this baseline"
        exit 0
    else
        echo -e "${RED}❌ Failed to create baseline${NC}"
        exit 1
    fi
fi

# Run regression check
echo ""
echo -e "${BLUE}📊 Checking for quality regressions...${NC}"

# Execute regression check
# Build command with conditional flags
REGRESSION_FLAGS="--baseline ${BASELINE_PATH} --path . --format table"
if [ -n "${MAX_SCORE_DROP}" ]; then
    REGRESSION_FLAGS="${REGRESSION_FLAGS} --max-score-drop ${MAX_SCORE_DROP}"
fi
if [ "${ALLOW_GRADE_DROP}" = "true" ]; then
    REGRESSION_FLAGS="${REGRESSION_FLAGS} --allow-grade-drop"
fi
if [ "${BLOCK_ON_REGRESSION}" = "true" ] && [ "${MODE}" = "strict" ]; then
    REGRESSION_FLAGS="${REGRESSION_FLAGS} --fail-on-regression"
fi

if pmat tdg check-regression ${REGRESSION_FLAGS}; then
    echo -e "${GREEN}✅ No quality regressions detected${NC}"
else
    REGRESSION_EXIT=$?

    if [ "${MODE}" = "warning" ]; then
        echo -e "${YELLOW}⚠️  Quality regression detected (warning mode)${NC}"
        echo "   Commit allowed but please review quality issues"
    else
        echo -e "${RED}❌ Quality regression detected - commit blocked${NC}"
        echo ""
        echo "To fix:"
        echo "  1. Review quality issues above"
        echo "  2. Improve code quality to meet standards"
        echo "  3. Or update baseline if changes are intentional:"
        echo "     pmat tdg baseline update --output \"${BASELINE_PATH}\""
        echo ""
        echo "To bypass (NOT RECOMMENDED):"
        echo "  git commit --no-verify"
        exit ${REGRESSION_EXIT}
    fi
fi

# Run quality check for new/modified files
echo ""
echo -e "${BLUE}📋 Checking quality of new/modified files...${NC}"

# Execute quality check
# Build command with conditional flags
QUALITY_FLAGS="--path . --format table --new-files-only --baseline ${BASELINE_PATH}"
if [ -n "${MIN_GRADE}" ]; then
    QUALITY_FLAGS="${QUALITY_FLAGS} --min-grade ${MIN_GRADE}"
fi
if [ "${BLOCK_ON_NEW_FILES}" = "true" ] && [ "${MODE}" = "strict" ]; then
    QUALITY_FLAGS="${QUALITY_FLAGS} --fail-on-violation"
fi

if pmat tdg check-quality ${QUALITY_FLAGS}; then
    echo -e "${GREEN}✅ All new/modified files meet quality standards${NC}"
else
    QUALITY_EXIT=$?

    if [ "${MODE}" = "warning" ]; then
        echo -e "${YELLOW}⚠️  Quality violations detected (warning mode)${NC}"
        echo "   Commit allowed but please improve code quality"
    else
        echo -e "${RED}❌ Quality violations detected - commit blocked${NC}"
        echo ""
        echo "New/modified files must meet minimum grade: ${MIN_GRADE}"
        echo ""
        echo "To fix:"
        echo "  1. Improve code quality for files listed above"
        echo "  2. Reduce complexity, add documentation, remove duplication"
        echo ""
        echo "To bypass (NOT RECOMMENDED):"
        echo "  git commit --no-verify"
        exit ${QUALITY_EXIT}
    fi
fi

# bashrs linting check (for bash scripts and Makefile)
echo ""
echo -e "${BLUE}🔍 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
                    # bashrs found errors - block commit
                    echo -e "${RED}❌ bashrs found errors in $file${NC}"
                    BASHRS_FAILED=1
                else
                    # Check for warnings (allowed but displayed)
                    WARNING_COUNT=$(bashrs lint "$file" 2>&1 | grep -o '[0-9]* warning(s)' | grep -o '[0-9]*' | head -1)
                    if [ -n "$WARNING_COUNT" ] && [ "$WARNING_COUNT" -gt 0 ]; then
                        echo -e "${YELLOW}   ⚠️  $WARNING_COUNT warning(s) (commit allowed)${NC}"
                    else
                        echo -e "   ✅ No issues"
                    fi
                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 linting passed${NC}"
    else
        echo "   No bash/Makefile files staged"
    fi
fi

# All checks passed
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${GREEN}✅ All quality gates passed (TDG + bashrs)${NC}"
echo ""

exit 0
