#!/bin/bash
# LDF Pre-Commit Hook - Auto-generated, do not edit manually
# Reinstall with: ldf hooks install
# Configuration: .ldf/config.yaml (hooks section)

# Colors
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m'

ERRORS=0
WARNINGS=0

{% if run_on_all_commits %}
RUN_SPEC_LINT=true
{% else %}
# Only run if spec files are staged
if git diff --cached --name-only | grep -q "^\.ldf/specs/"; then
    RUN_SPEC_LINT=true
else
    RUN_SPEC_LINT=false
fi
{% endif %}

# Spec Linting
{% if spec_lint %}
if [ "$RUN_SPEC_LINT" = true ]; then
    echo "Running LDF spec lint..."
    if command -v ldf &> /dev/null; then
        # Capture output to count warnings
        LINT_OUTPUT=$(ldf lint --all --format ci 2>&1)
        LINT_EXIT=$?

        # Display output
        echo "$LINT_OUTPUT"

        # Check for errors (non-zero exit code)
        if [ $LINT_EXIT -ne 0 ]; then
            ERRORS=$((ERRORS + 1))
        fi

        # Count warnings from output (for strict mode)
        WARN_COUNT=$(echo "$LINT_OUTPUT" | grep -c "^⚠ Warning:" || true)
        WARNINGS=$((WARNINGS + WARN_COUNT))
    else
        echo -e "${YELLOW}Note: ldf command not found, skipping spec lint${NC}"
    fi
fi
{% endif %}

# Python Linting
{% if python_enabled %}
PYTHON_FILES=$(git diff --cached --name-only | grep '\.py$' || true)
if [ -n "$PYTHON_FILES" ]; then
    echo "Running Python lint..."
    {% for tool in python_tools %}
    {% if tool == "ruff" %}
    if command -v ruff &> /dev/null; then
        echo "$PYTHON_FILES" | xargs ruff check || ERRORS=$((ERRORS + 1))
    else
        echo -e "${YELLOW}Warning: ruff not found, skipping${NC}"
    fi
    {% elif tool == "black" %}
    if command -v black &> /dev/null; then
        echo "$PYTHON_FILES" | xargs black --check || ERRORS=$((ERRORS + 1))
    else
        echo -e "${YELLOW}Warning: black not found, skipping${NC}"
    fi
    {% endif %}
    {% endfor %}
fi
{% endif %}

# TypeScript/JavaScript Linting
{% if typescript_enabled %}
TS_FILES=$(git diff --cached --name-only | grep -E '\.(ts|tsx|js|jsx)$' || true)
if [ -n "$TS_FILES" ]; then
    echo "Running TypeScript/JavaScript lint..."
    {% for tool in typescript_tools %}
    {% if tool == "eslint" %}
    if command -v npx &> /dev/null; then
        echo "$TS_FILES" | xargs npx eslint || ERRORS=$((ERRORS + 1))
    else
        echo -e "${YELLOW}Warning: npx not found, skipping eslint${NC}"
    fi
    {% endif %}
    {% endfor %}
fi
{% endif %}

# Go Linting
{% if go_enabled %}
GO_FILES=$(git diff --cached --name-only | grep '\.go$' || true)
if [ -n "$GO_FILES" ]; then
    echo "Running Go lint..."
    {% for tool in go_tools %}
    {% if tool == "golangci-lint" %}
    if command -v golangci-lint &> /dev/null; then
        golangci-lint run --new-from-rev=HEAD~1 || ERRORS=$((ERRORS + 1))
    else
        echo -e "${YELLOW}Warning: golangci-lint not found, skipping${NC}"
    fi
    {% endif %}
    {% endfor %}
fi
{% endif %}

# Results
{% if strict %}
# Strict mode: fail on both errors and warnings
if [ $ERRORS -gt 0 ] || [ $WARNINGS -gt 0 ]; then
    echo ""
    if [ $ERRORS -gt 0 ]; then
        echo -e "${RED}Pre-commit checks failed with $ERRORS error(s).${NC}"
    fi
    if [ $WARNINGS -gt 0 ]; then
        echo -e "${RED}Pre-commit checks failed with $WARNINGS warning(s) (strict mode).${NC}"
    fi
    echo "Fix issues and try again, or bypass with: git commit --no-verify"
    exit 1
fi
{% else %}
# Normal mode: fail only on errors, show warnings
if [ $ERRORS -gt 0 ]; then
    echo ""
    echo -e "${RED}Pre-commit checks failed with $ERRORS error(s).${NC}"
    echo "Fix errors and try again, or bypass with: git commit --no-verify"
    exit 1
fi

if [ $WARNINGS -gt 0 ]; then
    echo -e "${YELLOW}Pre-commit completed with $WARNINGS warning(s).${NC}"
fi
{% endif %}

echo -e "${GREEN}Pre-commit checks passed.${NC}"
exit 0
