<?php
include 'db.php';

echo "<h2>System Health Check</h2>";

// 0. PHP Configuration
echo "<h3>PHP Configuration</h3>";
echo "<ul>";
echo "<li>upload_max_filesize: " . ini_get('upload_max_filesize') . "</li>";
echo "<li>post_max_size: " . ini_get('post_max_size') . "</li>";
echo "<li>memory_limit: " . ini_get('memory_limit') . "</li>";
echo "<li>max_execution_time: " . ini_get('max_execution_time') . "s</li>";
echo "</ul>";

// 1. Check database tables
$tables = ['games', 'game_categories', 'videos'];
foreach ($tables as $table) {
    if ($result = $conn->query("SHOW TABLES LIKE '$table'")) {
        if ($result->num_rows > 0) {
            echo "<p style='color:green;'>SUCCESS: Table '$table' exists.</p>";
            // Check columns
            if ($cols = $conn->query("SHOW COLUMNS FROM $table")) {
                echo "<table border='1' style='border-collapse: collapse; margin-bottom: 20px;'>";
                echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Default</th><th>Extra</th></tr>";
                while ($col = $cols->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . $col['Field'] . "</td>";
                    echo "<td>" . $col['Type'] . "</td>";
                    echo "<td>" . $col['Null'] . "</td>";
                    echo "<td>" . ($col['Default'] ?? 'NULL') . "</td>";
                    echo "<td>" . $col['Extra'] . "</td>";
                    echo "</tr>";
                }
                echo "</table>";
            }
        } else {
            echo "<p style='color:red;'>FAILED: Table '$table' is MISSING!</p>";
        }
    } else {
        echo "<p style='color:red;'>ERROR checking table '$table': " . $conn->error . "</p>";
    }
}

// 2. Check directories
$root_dir = dirname(dirname(__DIR__)) . '/'; // /public_html/ directory
$base_dir = dirname(__DIR__) . '/'; // gamehub/ directory
$admin_dir = __DIR__ . '/'; // gamehub/admin/ directory

$dirs = [
    'Games' => $base_dir . 'games/',
    'Icons' => $root_dir . 'assets/icons/',
    'Videos' => $root_dir . 'assets/videos/',
    'Event Uploads' => $admin_dir . 'uploads/gaming_event/'
];

foreach ($dirs as $label => $path) {
    if (is_dir($path)) {
        echo "<p style='color:green;'>SUCCESS: $label directory exists.</p>";
        if (is_writable($path)) {
            echo "<p style='color:green;'>SUCCESS: $label directory is WRITABLE.</p>";
        } else {
            echo "<p style='color:red;'>FAILED: $label directory is NOT WRITABLE! ($path)</p>";
        }
    } else {
        echo "<p style='color:red;'>FAILED: $label directory is MISSING! ($path)</p>";
    }
}

echo "<h3>Latest 20 Games</h3>";
if ($result = $conn->query("SELECT game_id, game_name, game_icon, download_link, package_name FROM games ORDER BY game_id DESC LIMIT 20")) {
    echo "<table border='1' style='border-collapse: collapse;'>";
    echo "<tr><th>ID</th><th>Name</th><th>Icon</th><th>Link</th><th>Package Name</th></tr>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . $row['game_id'] . "</td>";
        echo "<td>" . $row['game_name'] . "</td>";
        echo "<td>" . $row['game_icon'] . "</td>";
        echo "<td>" . $row['download_link'] . "</td>";
        echo "<td>" . $row['package_name'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "<p style='color:red;'>Error fetching games: " . $conn->error . "</p>";
}

echo "<h3>Check Complete</h3>";
?>