#!/bin/bash

# Template Validation Script
# Prevents templates from going live without proper structure

# set -e  # Disabled to allow script to continue on errors

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
TEMPLATES_DIR="$PROJECT_DIR/templates"

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

echo -e "${BLUE}=== WordPress Template Validation ===${NC}"
echo "Scanning: $TEMPLATES_DIR"
echo ""

total_files=0
passed_files=0
failed_files=0

# Function to validate a single template
validate_template() {
    local file=$1
    local filename=$(basename "$file")
    local issues=()
    
    echo -n "Validating $filename: "
    
    # Check 1: Has get_header() call
    if ! grep -q "get_header()" "$file"; then
        issues+=("Missing get_header() call")
    fi
    
    # Check 2: Has get_footer() call  
    if ! grep -q "get_footer()" "$file"; then
        issues+=("Missing get_footer() call")
    fi
    
    # Check 3: Has ABSPATH security check
    if ! grep -q "ABSPATH" "$file"; then
        issues+=("Missing ABSPATH security check")
    fi
    
    # Check 4: Uses proper WordPress container structure
    if ! grep -q "content-area\|site-main" "$file"; then
        issues+=("Missing proper WordPress container structure")
    fi
    
    # Check 5: Check for inline AJAX (should use wp_ajax hooks)
    if grep -q "wp_ajax\|DOING_AJAX" "$file" && ! grep -q "wp_ajax_" "$file"; then
        issues+=("Uses inline AJAX instead of proper wp_ajax hooks")
    fi
    
    # Check 6: Has proper template header
    if ! grep -q "Template Name:" "$file"; then
        issues+=("Missing Template Name header")
    fi
    
    if [ ${#issues[@]} -eq 0 ]; then
        echo -e "${GREEN}✅ PASSED${NC}"
        ((passed_files++))
    else
        echo -e "${RED}❌ FAILED${NC}"
        ((failed_files++))
        for issue in "${issues[@]}"; do
            echo -e "   ${RED}→ $issue${NC}"
        done
    fi
    
    ((total_files++))
}

# Find and validate all template files
if [ -d "$TEMPLATES_DIR" ]; then
    # Find all PHP files first, then filter
    while IFS= read -r file; do
        filename=$(basename "$file")
        # Skip non-template files
        if [[ "$filename" == template-* ]] || [[ "$filename" == page-* ]] || [[ "$filename" == single-* ]]; then
            validate_template "$file"
        fi
    done < <(find "$TEMPLATES_DIR" -name "*.php" -type f)
else
    echo -e "${RED}Templates directory not found: $TEMPLATES_DIR${NC}"
    exit 1
fi

echo ""
echo -e "${BLUE}=== Validation Summary ===${NC}"
echo "Total templates: $total_files"
echo -e "Passed: ${GREEN}$passed_files${NC}"
echo -e "Failed: ${RED}$failed_files${NC}"

if [ $failed_files -gt 0 ]; then
    echo ""
    echo -e "${RED}🚨 VALIDATION FAILED!${NC}"
    echo "Fix the issues above before deploying templates."
    echo "See TEMPLATE_VALIDATION_GUIDE.md for requirements."
    exit 1
elif [ $total_files -eq 0 ]; then
    echo ""
    echo -e "${YELLOW}⚠️  No templates found to validate${NC}"
    exit 0
else
    echo ""
    echo -e "${GREEN}🎉 ALL TEMPLATES PASSED VALIDATION!${NC}"
    exit 0
fi