<?php
/**
 * Emporio Sport - Implementation Verification Script
 * This script verifies that all changes have been properly implemented
 */

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$checks = [];
$warnings = [];
$errors = [];

// 1. Check database configuration
$db_configured = false;
if (file_exists(__DIR__ . '/db_config.php')) {
    ob_start();
    require_once __DIR__ . '/db_config.php';
    ob_end_clean();
    
    if (isset($conn)) {
        if ($conn->connect_error) {
            $errors[] = "Database connection failed: " . $conn->connect_error;
        } else {
            $checks[] = "✓ Database configured and connected";
            $db_configured = true;
        }
    }
}

// 2. Check configuration files
$configChecks = [
    'Site Config' => __DIR__ . '/data/site_config.json',
    'Header Config' => __DIR__ . '/data/header_config.json'
];

foreach ($configChecks as $name => $path) {
    if (file_exists($path)) {
        $config = json_decode(file_get_contents($path), true);
        if ($config === null) {
            $errors[] = "$name JSON is malformed";
        } else {
            $checks[] = "✓ $name exists and is valid";
        }
    } else {
        $errors[] = "$name not found at $path";
    }
}

// 3. Check helper files
$helperChecks = [
    'Meta Manager' => __DIR__ . '/helpers/meta_manager.php',
    'Product Image Manager' => __DIR__ . '/helpers/product_image_manager.php'
];

foreach ($helperChecks as $name => $path) {
    if (file_exists($path)) {
        $checks[] = "✓ $name exists";
    } else {
        $errors[] = "$name not found at $path";
    }
}

// 4. Check .htaccess configuration
if (file_exists(__DIR__ . '/.htaccess')) {
    $htaccess = file_get_contents(__DIR__ . '/.htaccess');
    if (strpos($htaccess, 'RewriteCond %{REQUEST_FILENAME}.php -f') !== false) {
        $checks[] = "✓ .htaccess URL rewriting configured";
    } else {
        $warnings[] = "⚠ .htaccess may not have proper URL rewriting rules";
    }
} else {
    $errors[] = ".htaccess file not found";
}

// 5. Check sport-pics folder
if (is_dir(__DIR__ . '/sport-pics')) {
    $imageCount = count(array_diff(scandir(__DIR__ . '/sport-pics'), ['.', '..']));
    if ($imageCount > 0) {
        $checks[] = "✓ sport-pics folder exists with $imageCount images";
    } else {
        $warnings[] = "⚠ sport-pics folder exists but is empty";
    }
} else {
    $errors[] = "sport-pics folder not found";
}

// 6. Check if logo exists
if (file_exists(__DIR__ . '/sport-pics/logo-1.png')) {
    $checks[] = "✓ Logo file found (logo-1.png)";
} else {
    $warnings[] = "⚠ Logo file not found at sport-pics/logo-1.png";
}

// 7. Load and verify site config
$siteConfig = [];
if (file_exists(__DIR__ . '/data/site_config.json')) {
    $siteConfig = json_decode(file_get_contents(__DIR__ . '/data/site_config.json'), true) ?? [];
}

// 8. Check branding updates
if (!empty($siteConfig['title']) && strpos($siteConfig['title'], 'Emporio Sport') !== false) {
    $checks[] = "✓ Brand name updated to 'Emporio Sport'";
} else {
    $errors[] = "Brand name not properly updated in site config";
}

// 9. Check colors
$requiredColors = ['primary_color', 'secondary_color', 'footer_bg_color'];
foreach ($requiredColors as $color) {
    if (!empty($siteConfig[$color])) {
        $checks[] = "✓ Color configuration found: {$color}";
    } else {
        $warnings[] = "⚠ Color not configured: $color";
    }
}

// 10. Check meta information
if (!empty($siteConfig['meta_title_suffix']) && !empty($siteConfig['meta_description_default'])) {
    $checks[] = "✓ Meta tags configuration found";
} else {
    $warnings[] = "⚠ Meta tags may not be fully configured";
}

// 11. Check language and currency configuration
$headerConfig = [];
if (file_exists(__DIR__ . '/data/header_config.json')) {
    $headerConfig = json_decode(file_get_contents(__DIR__ . '/data/header_config.json'), true) ?? [];
}

if (!empty($headerConfig['languages'])) {
    $langCount = count($headerConfig['languages']);
    if (in_array('it', array_column($headerConfig['languages'], 'code'))) {
        $checks[] = "✓ Italian language configured (default)";
    } else {
        $warnings[] = "⚠ Italian language not found as default";
    }
    $checks[] = "✓ Total languages configured: $langCount";
} else {
    $errors[] = "Languages not configured";
}

if (!empty($headerConfig['currencies'])) {
    $currCount = count($headerConfig['currencies']);
    if (in_array('eur', array_column($headerConfig['currencies'], 'code'))) {
        $checks[] = "✓ EUR currency configured (default)";
    } else {
        $warnings[] = "⚠ EUR currency not found as default";
    }
    $checks[] = "✓ Total currencies configured: $currCount";
} else {
    $errors[] = "Currencies not configured";
}

// 12. Check API endpoint
if (file_exists(__DIR__ . '/api_set_preference.php')) {
    $apiContent = file_get_contents(__DIR__ . '/api_set_preference.php');
    if (strpos($apiContent, 'exchange_rate') !== false && strpos($apiContent, 'exchangeRates') !== false) {
        $checks[] = "✓ API for preferences enhanced with exchange rates";
    } else {
        $warnings[] = "⚠ API may not have enhanced preference handling";
    }
} else {
    $errors[] = "api_set_preference.php not found";
}

// 13. Check index.php meta manager integration
if (file_exists(__DIR__ . '/index.php')) {
    $indexContent = file_get_contents(__DIR__ . '/index.php');
    if (strpos($indexContent, 'meta_manager.php') !== false || strpos($indexContent, 'renderMetaTags') !== false) {
        $checks[] = "✓ Meta manager integrated in index.php";
    } else {
        $warnings[] = "⚠ Meta manager may not be integrated in index.php";
    }
}

// 14. Check if mod_rewrite is likely available
$htaccess_exists = file_exists(__DIR__ . '/.htaccess');
if ($htaccess_exists && function_exists('apache_get_modules')) {
    $modules = apache_get_modules();
    if (in_array('mod_rewrite', $modules)) {
        $checks[] = "✓ mod_rewrite is enabled in Apache";
    } else {
        $errors[] = "mod_rewrite is not enabled - URL rewriting may not work";
    }
} else if ($htaccess_exists) {
    $warnings[] = "⚠ Cannot verify mod_rewrite status - may need manual verification";
}

// 15. Check database credentials
$hostname = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost';
$is_local = (strpos($hostname, 'localhost') !== false || strpos($hostname, '127.0.0.1') !== false);

$checks[] = "✓ Environment detected: " . ($is_local ? 'LOCALHOST (Development)' : 'CPANEL (Production)');

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Emporio Sport - Implementation Verification</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            padding: 40px;
            max-width: 700px;
            width: 100%;
        }
        h1 {
            color: #1a1a1a;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .status {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            margin-bottom: 20px;
        }
        .status.success {
            background: #d4edda;
            color: #155724;
        }
        .status.warning {
            background: #fff3cd;
            color: #856404;
        }
        .status.error {
            background: #f8d7da;
            color: #721c24;
        }
        .section {
            margin-bottom: 30px;
        }
        .section h2 {
            font-size: 16px;
            color: #333;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f0f0f0;
        }
        .item {
            padding: 12px;
            margin-bottom: 8px;
            border-radius: 6px;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .item.check {
            background: #f0f9ff;
            color: #0369a1;
            border-left: 4px solid #0369a1;
        }
        .item.warning {
            background: #fffaf0;
            color: #b45309;
            border-left: 4px solid #f59e0b;
        }
        .item.error {
            background: #fef2f2;
            color: #991b1b;
            border-left: 4px solid #dc2626;
        }
        .icon {
            font-size: 18px;
            font-weight: bold;
            min-width: 20px;
        }
        code {
            background: #f5f5f5;
            padding: 2px 6px;
            border-radius: 3px;
            font-family: 'Monaco', 'Courier New', monospace;
            font-size: 12px;
        }
        .stats {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 15px;
            margin-bottom: 30px;
        }
        .stat {
            text-align: center;
            padding: 15px;
            border-radius: 8px;
            background: #f5f5f5;
        }
        .stat-value {
            font-size: 28px;
            font-weight: bold;
            color: #1a1a1a;
        }
        .stat-label {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
        .footer {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #eee;
            font-size: 12px;
            color: #999;
            text-align: center;
        }
        .next-steps {
            background: #f0f9ff;
            padding: 15px;
            border-radius: 8px;
            margin-top: 20px;
            border-left: 4px solid #0369a1;
        }
        .next-steps h3 {
            color: #0369a1;
            font-size: 14px;
            margin-bottom: 10px;
        }
        .next-steps ul {
            margin-left: 20px;
            font-size: 13px;
            color: #333;
            line-height: 1.6;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>
            <span style="font-size: 28px;">🏀</span>
            Emporio Sport - Implementation Status
        </h1>
        
        <div class="status <?php echo (empty($errors) ? 'success' : (empty($checks) ? 'error' : 'warning')); ?>">
            <?php 
            if (empty($errors) && count($checks) >= 15) {
                echo '✓ IMPLEMENTATION COMPLETE';
            } elseif (empty($errors)) {
                echo '⚠ MOSTLY COMPLETE - Minor warnings';
            } else {
                echo '✗ NEEDS ATTENTION - Errors found';
            }
            ?>
        </div>
        
        <div class="stats">
            <div class="stat">
                <div class="stat-value" style="color: #10b981;"><?php echo count($checks); ?></div>
                <div class="stat-label">Checks Passed</div>
            </div>
            <div class="stat">
                <div class="stat-value" style="color: #f59e0b;"><?php echo count($warnings); ?></div>
                <div class="stat-label">Warnings</div>
            </div>
            <div class="stat">
                <div class="stat-value" style="color: #ef4444;"><?php echo count($errors); ?></div>
                <div class="stat-label">Errors</div>
            </div>
        </div>
        
        <?php if (!empty($checks)): ?>
        <div class="section">
            <h2>✓ Completed Items</h2>
            <?php foreach ($checks as $check): ?>
            <div class="item check">
                <span class="icon">✓</span>
                <span><?php echo htmlspecialchars($check); ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
        
        <?php if (!empty($warnings)): ?>
        <div class="section">
            <h2>⚠ Warnings</h2>
            <?php foreach ($warnings as $warning): ?>
            <div class="item warning">
                <span class="icon">⚠</span>
                <span><?php echo htmlspecialchars($warning); ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
        
        <?php if (!empty($errors)): ?>
        <div class="section">
            <h2>✗ Errors</h2>
            <?php foreach ($errors as $error): ?>
            <div class="item error">
                <span class="icon">✗</span>
                <span><?php echo htmlspecialchars($error); ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
        
        <?php if (empty($errors)): ?>
        <div class="next-steps">
            <h3>🚀 Next Steps</h3>
            <ul>
                <li>Test all pages on both localhost and cPanel</li>
                <li>Verify URL rewriting (access pages without .php extension)</li>
                <li>Test currency and language switchers</li>
                <li>Check meta titles in page source</li>
                <li>Review color theme across all pages</li>
                <li>Test product images from sport-pics folder</li>
                <li>Verify database connection on both environments</li>
                <li>Run performance tests</li>
                <li>Check mobile responsiveness</li>
                <li>Deploy to production when ready</li>
            </ul>
        </div>
        <?php endif; ?>
        
        <div class="footer">
            <strong>Environment:</strong> <?php echo ($is_local ? 'LOCALHOST (XAMPP)' : 'CPANEL (Production)'); ?><br>
            <strong>Verification Date:</strong> <?php echo date('Y-m-d H:i:s'); ?><br>
            <strong>PHP Version:</strong> <?php echo phpversion(); ?>
        </div>
    </div>
</body>
</html>
