#!/bin/bash
# =====================================================================
# main-page-test.sh - Test the main page functionality
# =====================================================================
# This script checks if the main page loads correctly
# =====================================================================

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

echo "=== Testing Main Page ==="
echo "Using base URL: $BASE_URL"

# Check if the main page loads properly
echo "Checking if the main page loads properly..."
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/index.html")

if [ "$RESPONSE" -eq 200 ]; then
    echo "✅ Main page loads successfully (HTTP $RESPONSE)"
else
    echo "❌ Main page failed to load (HTTP $RESPONSE)"
    exit 1
fi

# Check for page title
echo "Checking page title..."
TITLE=$(curl -s "$BASE_URL/index.html" | grep -o "<title>.*</title>")

if [ -n "$TITLE" ]; then
    echo "✅ Page title found: $TITLE"
else
    echo "❌ Page title not found"
    exit 1
fi

# Check for CSS loading
echo "Checking if CSS loads properly..."
CSS_LINK=$(curl -s "$BASE_URL/index.html" | grep -o '<link[^>]*href="[^"]*styles.css[^"]*"[^>]*>')

if [ -n "$CSS_LINK" ]; then
    echo "✅ CSS link found: $CSS_LINK"
    
    # Check if the CSS file itself loads
    CSS_URL=$(echo "$CSS_LINK" | grep -o 'href="[^"]*"' | sed 's/href="//;s/"$//')
    CSS_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/$CSS_URL")
    
    if [ "$CSS_RESPONSE" -eq 200 ]; then
        echo "✅ CSS file loads successfully (HTTP $CSS_RESPONSE)"
    else
        echo "❌ CSS file failed to load (HTTP $CSS_RESPONSE)"
        exit 1
    fi
else
    echo "❌ CSS link not found"
    exit 1
fi

echo "=== Main Page Test Completed Successfully ==="
exit 0
