#!/usr/bin/env bash
set -e

echo "========================================"
echo "⚡ Fast Pre-Commit Tests & Coverage"
echo "========================================"

# Pre-commit passes staged files as arguments
STAGED_FILES="$@"
TEST_TARGETS=""

if [ -z "$STAGED_FILES" ]; then
    echo "No files provided. Running default unit tests."
    TEST_TARGETS="tests/unit"
else
    for file in $STAGED_FILES; do
        if [[ "$file" == tests/* ]]; then
            TEST_TARGETS="$TEST_TARGETS $file"
        elif [[ "$file" == GramAddict/* ]]; then
            filename=$(basename "$file")
            # Heuristic: Try to find a matching unit test
            test_file="tests/unit/test_${filename}"
            core_test_file="tests/core/test_${filename}"
            if [ -f "$test_file" ]; then
                TEST_TARGETS="$TEST_TARGETS $test_file"
            elif [ -f "$core_test_file" ]; then
                TEST_TARGETS="$TEST_TARGETS $core_test_file"
            else
                # Try to find matching e2e tests by searching for each word in the module name
                module_name="${filename%.py}"
                e2e_matches=""
                for word in $(echo "$module_name" | tr '_' '\n'); do
                    if [ ${#word} -ge 4 ]; then  # Only search meaningful words (4+ chars)
                        found=$(find tests/e2e -name "test_*${word}*.py" 2>/dev/null | head -3)
                        if [ -n "$found" ]; then
                            e2e_matches="$e2e_matches $found"
                        fi
                    fi
                done
                e2e_matches=$(echo "$e2e_matches" | xargs -n1 2>/dev/null | sort -u | head -3 | xargs 2>/dev/null)
                if [ -n "$e2e_matches" ]; then
                    echo "⚠️ No direct unit test for $file, using matching E2E tests: $e2e_matches"
                    TEST_TARGETS="$TEST_TARGETS $e2e_matches"
                else
                    echo "⚠️ No direct unit test found for $file, falling back to all unit tests."
                    TEST_TARGETS="tests/unit"
                    break
                fi
            fi
        fi
    done
fi

# Trim whitespace
TEST_TARGETS=$(echo "$TEST_TARGETS" | xargs)

if [ -z "$TEST_TARGETS" ]; then
    echo "No Python files changed that require testing. Skipping."
    exit 0
fi

echo "🧪 Running tests on: $TEST_TARGETS"
PYTHONPATH=. venv/bin/pytest $TEST_TARGETS --cov=GramAddict --cov-report=xml -q

echo ""
echo "========================================"
echo "🛡️  Checking Coverage of NEW lines (diff-cover)"
echo "========================================"

# Check if origin/main exists, otherwise use main or HEAD
COMPARE_BRANCH="origin/main"
if ! git rev-parse --verify "$COMPARE_BRANCH" >/dev/null 2>&1; then
    COMPARE_BRANCH="main"
fi
if ! git rev-parse --verify "$COMPARE_BRANCH" >/dev/null 2>&1; then
    COMPARE_BRANCH="HEAD"
fi

# Run diff-cover requiring 30% coverage on new/changed lines
venv/bin/diff-cover coverage.xml --compare-branch=$COMPARE_BRANCH --fail-under=25

echo "✅ All targeted tests passed and coverage is sufficient on new lines!"
