#!/bin/bash
# =====================================================================
# includes-test.sh - Test the includes functionality
# =====================================================================
# This script checks if the includes system is working properly
# =====================================================================

# Check if base URL is provided
if [ -z "$1" ]; then
  BASE_URL="http://localhost:8080"
else
  BASE_URL="$1"
fi

echo "=== Testing Includes Functionality ==="
echo "Using base URL: $BASE_URL"

# Array to track failures
FAILURES=0

# Test if includes.js exists and loads properly
echo "Checking if includes.js exists and loads properly..."
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/includes.js")
if [ "$RESPONSE" -eq 200 ]; then
    echo "✅ includes.js loads successfully (HTTP $RESPONSE)"
    
    # Check includes.js content
    INCLUDES_JS=$(curl -s "$BASE_URL/includes.js")
    if echo "$INCLUDES_JS" | grep -q "includeHTML"; then
        echo "✅ includes.js contains includeHTML function"
    else
        echo "❌ includes.js doesn't contain includeHTML function"
        FAILURES=$((FAILURES+1))
    fi
    
    if echo "$INCLUDES_JS" | grep -q "DOMContentLoaded"; then
        echo "✅ includes.js contains DOMContentLoaded event listener"
    else
        echo "❌ includes.js doesn't contain DOMContentLoaded event listener"
        FAILURES=$((FAILURES+1))
    fi
else
    echo "❌ includes.js failed to load (HTTP $RESPONSE)"
    FAILURES=$((FAILURES+1))
fi

# Check if the includes/header.html file exists
echo "Checking includes/header.html file..."
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/includes/header.html")
if [ "$RESPONSE" -eq 200 ] || [ "$RESPONSE" -eq 403 ]; then
    echo "✅ includes/header.html file exists (HTTP $RESPONSE)"
else
    echo "❌ includes/header.html file doesn't exist (HTTP $RESPONSE)"
    FAILURES=$((FAILURES+1))
fi

# Check if pages with includes load correctly
echo "Checking pages that use includes..."
INCLUDE_PAGES=(
    "template-with-includes.html"
    "stories/story-with-includes.html"
    "one-pager-tools/tool-with-includes.html"
)

for page in "${INCLUDE_PAGES[@]}"; do
    RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/$page")
    if [ "$RESPONSE" -eq 200 ]; then
        echo "✅ $page loads successfully (HTTP $RESPONSE)"
        
        # Check if the page has include placeholders
        CONTENT=$(curl -s "$BASE_URL/$page")
        if echo "$CONTENT" | grep -q "id=\"header-include\""; then
            echo "✅ $page has header include placeholder"
        else
            echo "❌ $page doesn't have header include placeholder"
            FAILURES=$((FAILURES+1))
        fi
        
        if echo "$CONTENT" | grep -q "id=\"footer-include\""; then
            echo "✅ $page has footer include placeholder"
        else
            echo "❌ $page doesn't have footer include placeholder"
            FAILURES=$((FAILURES+1))
        fi
        
        # Check if includes.js is included in the page
        if echo "$CONTENT" | grep -q "includes.js"; then
            echo "✅ $page includes the includes.js script"
        else
            echo "❌ $page doesn't include the includes.js script"
            FAILURES=$((FAILURES+1))
        fi
    else
        echo "❌ $page failed to load (HTTP $RESPONSE)"
        FAILURES=$((FAILURES+1))
    fi
    
    echo "---"
done

# Check if any failures occurred
if [ "$FAILURES" -eq 0 ]; then
    echo "=== All Includes Tests Passed ==="
    exit 0
else
    echo "=== Includes Tests Failed: $FAILURES failures ==="
    exit 1
fi
