#!/bin/bash
# =====================================================================
# theme-test.sh - Test the theme functionality
# =====================================================================
# This script checks if the theme 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 Theme Functionality ==="
echo "Using base URL: $BASE_URL"

# Array to track failures
FAILURES=0

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

# Check if CSS has theme-related styles
echo "Checking if styles.css has theme-related styles..."
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/styles.css")
if [ "$RESPONSE" -eq 200 ]; then
    echo "✅ styles.css loads successfully (HTTP $RESPONSE)"
    
    # Check styles.css content for theme-related styles
    STYLES_CSS=$(curl -s "$BASE_URL/styles.css")
    if echo "$STYLES_CSS" | grep -q "data-theme"; then
        echo "✅ styles.css contains data-theme attribute selectors"
    else
        echo "❌ styles.css doesn't contain data-theme attribute selectors"
        FAILURES=$((FAILURES+1))
    fi
    
    # Check for dark mode styles
    if echo "$STYLES_CSS" | grep -q "dark"; then
        echo "✅ styles.css contains dark mode styles"
    else
        echo "❌ styles.css doesn't contain dark mode styles"
        FAILURES=$((FAILURES+1))
    fi
else
    echo "❌ styles.css failed to load (HTTP $RESPONSE)"
    FAILURES=$((FAILURES+1))
fi

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