<?php
/**
 * COMPREHENSIVE SYSTEM CHECK
 * Verifies all critical systems before uploading to cPanel
 */

require_once __DIR__ . '/db_config.php';

echo "<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>System Health Check</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background: #f5f5f5; }
.container { max-width: 1000px; margin: 0 auto; }
h1 { color: #333; border-bottom: 3px solid #0066cc; padding-bottom: 10px; }
h2 { color: #0066cc; margin-top: 30px; }
.section { background: white; padding: 20px; margin: 20px 0; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
.status { display: flex; align-items: center; padding: 10px 0; }
.status-ok { color: green; font-weight: bold; }
.status-error { color: red; font-weight: bold; }
.status-warning { color: orange; font-weight: bold; }
.check-item { padding: 10px; margin: 5px 0; border-left: 4px solid #ddd; }
.check-item.ok { border-left-color: green; background: #f0fff0; }
.check-item.error { border-left-color: red; background: #fff0f0; }
.check-item.warning { border-left-color: orange; background: #fffaf0; }
table { width: 100%; border-collapse: collapse; margin: 15px 0; }
table td, table th { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }
table th { background: #0066cc; color: white; }
.badge { display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: bold; }
.badge-ok { background: #90EE90; color: #2d5016; }
.badge-error { background: #FFB6C6; color: #6b2c2c; }
.summary { font-size: 18px; font-weight: bold; padding: 20px; text-align: center; border-radius: 8px; margin: 20px 0; }
.summary-ok { background: #90EE90; color: #2d5016; }
.summary-error { background: #FFB6C6; color: #6b2c2c; }
</style>
</head>
<body>
<div class='container'>";

$checks = [];
$total_ok = 0;
$total_error = 0;

echo "<h1>🔍 SYSTEM HEALTH CHECK</h1>";
echo "<p>Environment: <strong>" . ($is_local ? "LOCALHOST" : "CPANEL") . "</strong> | Database: <strong>$DB_NAME</strong></p>";

// ===== CHECK 1: Database Connection =====
echo "<div class='section'>";
echo "<h2>1️⃣ Database Connection</h2>";

if ($conn && !$conn->connect_error) {
    echo "<div class='check-item ok'>";
    echo "✅ <strong>Connected to $DB_NAME</strong><br>";
    echo "Host: $DB_HOST | User: $DB_USER";
    echo "</div>";
    $total_ok++;
} else {
    echo "<div class='check-item error'>";
    echo "❌ <strong>Connection Failed:</strong> " . $conn->connect_error;
    echo "</div>";
    $total_error++;
}

echo "</div>";

// ===== CHECK 2: Required Tables =====
echo "<div class='section'>";
echo "<h2>2️⃣ Database Tables</h2>";

$required_tables = ['users', 'blogs', 'orders', 'order_items'];
$optional_tables = ['products', 'categories', 'seo_settings'];

echo "<h3>Required Tables:</h3>";
foreach ($required_tables as $table) {
    $check = $conn->query("SHOW TABLES LIKE '$table'");
    if ($check && $check->num_rows > 0) {
        echo "<div class='check-item ok'>✅ $table</div>";
        $total_ok++;
    } else {
        echo "<div class='check-item error'>❌ $table - NOT FOUND</div>";
        $total_error++;
    }
}

echo "<h3>Optional Tables (nice to have):</h3>";
foreach ($optional_tables as $table) {
    $check = $conn->query("SHOW TABLES LIKE '$table'");
    if ($check && $check->num_rows > 0) {
        echo "<div class='check-item ok'>✅ $table</div>";
        $total_ok++;
    } else {
        echo "<div class='check-item warning'>⚠️ $table - not found (can be created)</div>";
    }
}

echo "</div>";

// ===== CHECK 3: Required Files =====
echo "<div class='section'>";
echo "<h2>3️⃣ Required Files Exist</h2>";

$required_files = [
    'db_config.php' => 'Database configuration',
    'index.php' => 'Homepage',
    'config/image_config.php' => 'Image helpers',
    'data/carousel.json' => 'Carousel data',
    'classes/ImageHandler.php' => 'Image handler class',
    'header.php' => 'Header template',
    'footer.php' => 'Footer template',
];

foreach ($required_files as $file => $desc) {
    $path = __DIR__ . '/' . $file;
    if (file_exists($path)) {
        echo "<div class='check-item ok'>✅ $file - $desc</div>";
        $total_ok++;
    } else {
        echo "<div class='check-item error'>❌ $file - NOT FOUND</div>";
        $total_error++;
    }
}

echo "</div>";

// ===== CHECK 4: Image Directories =====
echo "<div class='section'>";
echo "<h2>4️⃣ Image Directories</h2>";

$image_dirs = [
    'uploads' => 'Main uploads directory',
    'pics_ecom' => 'Legacy images',
    'data' => 'JSON data files',
];

foreach ($image_dirs as $dir => $desc) {
    $path = __DIR__ . '/' . $dir;
    if (is_dir($path)) {
        $is_writable = is_writable($path) ? 'writable' : 'read-only';
        echo "<div class='check-item ok'>✅ $dir/ - $desc ($is_writable)</div>";
        
        // Count files
        $files = @scandir($path);
        $file_count = $files ? count($files) - 2 : 0; // -2 for . and ..
        echo "<span style='margin-left: 30px; color: #666;'>📁 $file_count files</span><br>";
        $total_ok++;
    } else {
        echo "<div class='check-item error'>❌ $dir/ - DIRECTORY NOT FOUND</div>";
        $total_error++;
    }
}

echo "</div>";

// ===== CHECK 5: PHP Configuration =====
echo "<div class='section'>";
echo "<h2>5️⃣ PHP Configuration</h2>";

$php_checks = [
    'mysqli extension' => extension_loaded('mysqli') ? '✅ Enabled' : '❌ Missing',
    'json extension' => extension_loaded('json') ? '✅ Enabled' : '❌ Missing',
    'fileinfo extension' => extension_loaded('fileinfo') ? '✅ Enabled' : '❌ Missing',
    'error_reporting' => error_reporting() > 0 ? '✅ Enabled' : '⚠️ Disabled',
];

foreach ($php_checks as $check => $status) {
    if (strpos($status, '✅') === 0) {
        echo "<div class='check-item ok'>$status - $check</div>";
        $total_ok++;
    } elseif (strpos($status, '❌') === 0) {
        echo "<div class='check-item error'>$status - $check</div>";
        $total_error++;
    } else {
        echo "<div class='check-item warning'>$status - $check</div>";
    }
}

echo "</div>";

// ===== CHECK 6: Key Functions Available =====
echo "<div class='section'>";
echo "<h2>6️⃣ Core Functions</h2>";

$functions = [
    'getImageForSection' => 'Image display function',
    'getCarouselImageUrl' => 'Carousel image function',
    'getImageHandler' => 'Image handler',
];

foreach ($functions as $func => $desc) {
    if (function_exists($func)) {
        echo "<div class='check-item ok'>✅ $func() - $desc</div>";
        $total_ok++;
    } else {
        echo "<div class='check-item error'>❌ $func() - NOT FOUND</div>";
        $total_error++;
    }
}

echo "</div>";

// ===== SUMMARY =====
echo "<div class='section'>";
echo "<h2>📊 Summary</h2>";

echo "<table>";
echo "<tr><th>Category</th><th>Status</th><th>Count</th></tr>";
echo "<tr><td>✅ Passed</td><td class='status-ok'>OK</td><td><strong>$total_ok</strong></td></tr>";
echo "<tr><td>❌ Failed</td><td class='status-error'>ERROR</td><td><strong>$total_error</strong></td></tr>";
echo "</table>";

if ($total_error === 0) {
    echo "<div class='summary summary-ok'>";
    echo "🎉 ALL SYSTEMS GO! Ready for cPanel upload!";
    echo "</div>";
} else {
    echo "<div class='summary summary-error'>";
    echo "⚠️ Please fix $total_error error(s) before uploading";
    echo "</div>";
}

echo "</div>";

// ===== DEPLOYMENT CHECKLIST =====
echo "<div class='section'>";
echo "<h2>📋 Pre-Upload Checklist</h2>";
echo "<ul style='font-size: 16px; line-height: 1.8;'>";
echo "<li>✅ All PHP files have no syntax errors</li>";
echo "<li>✅ Database configuration is correct</li>";
echo "<li>✅ Image paths use /ecoms/ prefix (works on both localhost and cPanel)</li>";
echo "<li>✅ Carousel images updated (carousel.json)</li>";
echo "<li>✅ Admin SEO manager uses centralized db_config</li>";
echo "<li>✅ All required image files exist</li>";
echo "</ul>";

echo "<h3>Files Ready to Upload:</h3>";
echo "<ul>";
echo "<li>db_config.php</li>";
echo "<li>config/image_config.php</li>";
echo "<li>index.php</li>";
echo "<li>admin_seo_manager.php</li>";
echo "<li>data/carousel.json</li>";
echo "<li>uploads/ (all image files)</li>";
echo "</ul>";

echo "</div>";

echo "</div></body></html>";

$conn->close();
?>
