#!/bin/bash

# Test script for the debug pre-commit hook
# This script helps verify that the hook is working correctly

set -e

echo "🧪 Testing TimeSafari Debug Pre-commit Hook"
echo "============================================="

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

# Test directory
TEST_DIR="$(mktemp -d)"
echo -e "${BLUE}Created test directory: $TEST_DIR${NC}"

# Function to cleanup
cleanup() {
    echo -e "${YELLOW}Cleaning up test directory...${NC}"
    rm -rf "$TEST_DIR"
}

# Set trap to cleanup on exit
trap cleanup EXIT

# Function to run test
run_test() {
    local test_name="$1"
    local test_file="$2"
    local expected_exit="$3"
    
    echo -e "\n${BLUE}Running test: $test_name${NC}"
    
    # Create test file
    echo "$test_file" > "$TEST_DIR/test.vue"
    
    # Stage the file
    cd "$TEST_DIR"
    git init > /dev/null 2>&1
    git add test.vue > /dev/null 2>&1
    
    # Run the hook
    if bash ../../.git/hooks/pre-commit > hook_output.txt 2>&1; then
        exit_code=0
    else
        exit_code=$?
    fi
    
    # Check result
    if [[ $exit_code -eq $expected_exit ]]; then
        echo -e "  ${GREEN}✅ PASS${NC} - Exit code: $exit_code (expected: $expected_exit)"
    else
        echo -e "  ${RED}❌ FAIL${NC} - Exit code: $exit_code (expected: $expected_exit)"
        echo -e "  ${YELLOW}Hook output:${NC}"
        cat hook_output.txt
    fi
    
    # Cleanup git
    rm -rf .git
    rm -f hook_output.txt
}

# Test cases
echo -e "\n${BLUE}Test Case 1: Clean file (should pass)${NC}"
run_test "Clean file" "// No debug code here" 0

echo -e "\n${BLUE}Test Case 2: Console statement (should fail)${NC}"
run_test "Console statement" "console.log('debug info')" 1

echo -e "\n${BLUE}Test Case 3: Debug template (should fail)${NC}"
run_test "Debug template" "Debug: {{ isMapReady ? 'Map Ready' : 'Map Loading' }}" 1

echo -e "\n${BLUE}Test Case 4: Debug constant (should fail)${NC}"
run_test "Debug constant" "const DEBUG_MODE = true" 1

echo -e "\n${BLUE}Test Case 5: Mixed content (should fail)${NC}"
run_test "Mixed content" "// Some normal code\nconsole.debug('test')\n// More normal code" 1

echo -e "\n${BLUE}Test Case 6: HTML debug comment (should fail)${NC}"
run_test "HTML debug comment" "<!-- debug: this is debug info -->" 1

echo -e "\n${BLUE}Test Case 7: Debug attribute (should fail)${NC}"
run_test "Debug attribute" "<div debug='true'>content</div>" 1

echo -e "\n${BLUE}Test Case 8: Test file (should be skipped)${NC}"
run_test "Test file" "console.log('this should be skipped')" 0

# Test branch detection
echo -e "\n${BLUE}Testing branch detection...${NC}"
cd "$TEST_DIR"
git init > /dev/null 2>&1
git checkout -b feature-branch > /dev/null 2>&1
echo "console.log('debug')" > test.vue
git add test.vue > /dev/null 2>&1

if bash ../../.git/hooks/pre-commit > hook_output.txt 2>&1; then
    echo -e "  ${GREEN}✅ PASS${NC} - Hook skipped on feature branch"
else
    echo -e "  ${RED}❌ FAIL${NC} - Hook should have been skipped on feature branch"
    echo -e "  ${YELLOW}Hook output:${NC}"
    cat hook_output.txt
fi

rm -rf .git
rm -f hook_output.txt

echo -e "\n${GREEN}🎉 All tests completed!${NC}"
echo -e "\n${BLUE}To test manually:${NC}"
echo "1. Make changes to a file with debug code"
echo "2. Stage the file: git add <filename>"
echo "3. Try to commit: git commit -m 'test'"
echo "4. The hook should prevent the commit if debug code is found"
