<?php
/**
 * Verification script for recent fixes
 * Checks:
 * 1. Favicon references updated
 * 2. Featured categories images exist
 * 3. Language/Currency API working
 * 4. Header configuration loaded correctly
 */

session_start();

// Color test - check if this is emporio-sport
echo "<h2>Emporio Sport - Fixes Verification</h2>";
echo "<p>Testing all recent fixes...</p>";

$errors = [];
$successes = [];

// 1. Check favicon in index.php
$indexContent = file_get_contents(__DIR__ . '/index.php');
if (strpos($indexContent, 'favicon.png') !== false && strpos($indexContent, 'link rel="icon"') !== false) {
    $errors[] = "❌ index.php still has favicon.png reference";
} else {
    $successes[] = "✅ index.php favicon updated correctly";
}

// 2. Check favicon in admin_dashboard.php
$adminContent = file_get_contents(__DIR__ . '/admin_dashboard.php');
if (strpos($adminContent, 'favicon.png') !== false && strpos($adminContent, 'link rel="icon"') !== false) {
    $errors[] = "❌ admin_dashboard.php still has favicon.png reference";
} else {
    $successes[] = "✅ admin_dashboard.php favicon updated correctly";
}

// 3. Check featured categories JSON
$jsonFile = __DIR__ . '/data/homepage_sections.json';
if (file_exists($jsonFile)) {
    $jsonData = json_decode(file_get_contents($jsonFile), true);
    if (isset($jsonData['featured_categories']) && is_array($jsonData['featured_categories'])) {
        if (count($jsonData['featured_categories']) > 0) {
            $successes[] = "✅ Featured categories JSON has " . count($jsonData['featured_categories']) . " items";
            // Check if images exist
            foreach ($jsonData['featured_categories'] as $idx => $cat) {
                $imgFile = __DIR__ . '/' . $cat['image'];
                if (file_exists($imgFile)) {
                    $successes[] = "✅ Featured category image exists: " . $cat['image'];
                } else {
                    $errors[] = "❌ Featured category image missing: " . $cat['image'];
                }
            }
        } else {
            $errors[] = "❌ Featured categories JSON is empty";
        }
    } else {
        $errors[] = "❌ Featured categories not found in JSON";
    }
} else {
    $errors[] = "❌ homepage_sections.json file not found";
}

// 4. Check header config
$headerConfigFile = __DIR__ . '/data/header_config.json';
if (file_exists($headerConfigFile)) {
    $headerConfig = json_decode(file_get_contents($headerConfigFile), true);
    if (isset($headerConfig['languages']) && isset($headerConfig['currencies'])) {
        $successes[] = "✅ Header config has " . count($headerConfig['languages']) . " languages and " . count($headerConfig['currencies']) . " currencies";
    } else {
        $errors[] = "❌ Header config missing languages or currencies";
    }
} else {
    $errors[] = "❌ header_config.json file not found";
}

// 5. Check api_set_preference.php
if (file_exists(__DIR__ . '/api_set_preference.php')) {
    $apiContent = file_get_contents(__DIR__ . '/api_set_preference.php');
    if (strpos($apiContent, 'selected_language') !== false && strpos($apiContent, 'selected_currency') !== false) {
        $successes[] = "✅ api_set_preference.php has language and currency handling";
    } else {
        $errors[] = "❌ api_set_preference.php missing language or currency logic";
    }
} else {
    $errors[] = "❌ api_set_preference.php file not found";
}

// 6. Check if logo exists
$logoPath = __DIR__ . '/sport-pics/logo-1.png';
if (file_exists($logoPath)) {
    $successes[] = "✅ Logo file exists at sport-pics/logo-1.png";
} else {
    $errors[] = "❌ Logo file missing at sport-pics/logo-1.png";
}

// 7. Check site_config.json
$siteConfigFile = __DIR__ . '/data/site_config.json';
if (file_exists($siteConfigFile)) {
    $siteConfig = json_decode(file_get_contents($siteConfigFile), true);
    if ($siteConfig['title'] === 'Emporio Sport') {
        $successes[] = "✅ Site title correctly set to 'Emporio Sport'";
    } else {
        $errors[] = "❌ Site title is not 'Emporio Sport', it's: " . ($siteConfig['title'] ?? 'unknown');
    }
} else {
    $errors[] = "❌ site_config.json file not found";
}

// Display results
echo "<div style='padding: 20px; font-family: Arial, sans-serif;'>";

if (!empty($successes)) {
    echo "<div style='background: #d4edda; border: 1px solid #c3e6cb; padding: 15px; margin: 10px 0; border-radius: 4px;'>";
    echo "<h3 style='margin-top: 0; color: #155724;'>Successful Tests (" . count($successes) . ")</h3>";
    foreach ($successes as $success) {
        echo "<p style='margin: 5px 0;'>" . htmlspecialchars($success) . "</p>";
    }
    echo "</div>";
}

if (!empty($errors)) {
    echo "<div style='background: #f8d7da; border: 1px solid #f5c6cb; padding: 15px; margin: 10px 0; border-radius: 4px;'>";
    echo "<h3 style='margin-top: 0; color: #721c24;'>Issues Found (" . count($errors) . ")</h3>";
    foreach ($errors as $error) {
        echo "<p style='margin: 5px 0;'>" . htmlspecialchars($error) . "</p>";
    }
    echo "</div>";
} else {
    echo "<div style='background: #d1ecf1; border: 1px solid #bee5eb; padding: 15px; margin: 10px 0; border-radius: 4px;'>";
    echo "<h3 style='margin-top: 0; color: #0c5460;'>✅ All Fixes Verified Successfully!</h3>";
    echo "</div>";
}

echo "</div>";

// Test API endpoint
echo "<div style='padding: 20px; margin-top: 20px; background: #f9f9f9; border-radius: 4px;'>";
echo "<h3>API Endpoint Test</h3>";
echo "<p>Current session language: " . ($_SESSION['selected_language'] ?? 'Not set') . "</p>";
echo "<p>Current session currency: " . ($_SESSION['selected_currency'] ?? 'Not set') . "</p>";
echo "<p>To test the API, try changing language/currency from the header dropdown.</p>";
echo "</div>";
?>
