<?php
/**
 * Diagnostic script to check if lounge menu files are properly set up
 * Access this from your browser: yourdomain.com/lounge/check-files.php
 */
?>
<!DOCTYPE html>
<html>
<head>
    <title>Lounge Files Diagnostic - Feliza Hotel</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background: #f5f5f5; }
        .container { max-width: 800px; margin: 0 auto; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
        h1 { color: #333; border-bottom: 2px solid #c5a059; padding-bottom: 10px; }
        .check { margin: 15px 0; padding: 10px; border-radius: 3px; }
        .pass { background: #d4edda; border-left: 4px solid #28a745; }
        .fail { background: #f8d7da; border-left: 4px solid #dc3545; }
        .info { background: #d1ecf1; border-left: 4px solid #17a2b8; }
        code { background: #f4f4f4; padding: 2px 6px; border-radius: 3px; font-family: monospace; }
        .solution { margin-top: 20px; padding: 15px; background: #fff3cd; border-left: 4px solid #ffc107; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔍 Lounge Menu Diagnostic Check</h1>

        <?php
        $checks = [];

        // Check 1: menu.json exists
        $menu_json_path = __DIR__ . '/menu.json';
        if (file_exists($menu_json_path)) {
            $checks[] = ['name' => 'menu.json File', 'status' => 'pass', 'message' => 'File exists at: ' . $menu_json_path];

            // Check 2: menu.json is readable
            if (is_readable($menu_json_path)) {
                $checks[] = ['name' => 'menu.json Readable', 'status' => 'pass', 'message' => 'File permissions are correct'];

                // Check 3: Valid JSON
                $content = file_get_contents($menu_json_path);
                $decoded = json_decode($content, true);
                if ($decoded !== null) {
                    $categoryCount = count($decoded['categories'] ?? []);
                    $checks[] = ['name' => 'Valid JSON', 'status' => 'pass', 'message' => 'JSON is valid with ' . $categoryCount . ' categories'];
                } else {
                    $checks[] = ['name' => 'Valid JSON', 'status' => 'fail', 'message' => 'JSON syntax error. Last JSON error: ' . json_last_error_msg()];
                }
            } else {
                $checks[] = ['name' => 'menu.json Readable', 'status' => 'fail', 'message' => 'File exists but is not readable. Check permissions.'];
            }
        } else {
            $checks[] = ['name' => 'menu.json File', 'status' => 'fail', 'message' => 'File NOT found at: ' . $menu_json_path];
        }

        // Check 4: index.php exists
        $index_php_path = __DIR__ . '/index.php';
        if (file_exists($index_php_path)) {
            $checks[] = ['name' => 'index.php File', 'status' => 'pass', 'message' => 'Main file exists'];
        } else {
            $checks[] = ['name' => 'index.php File', 'status' => 'fail', 'message' => 'Main file not found'];
        }

        // Check 5: CSS files
        $css_files = ['css/bootstrap.css', 'css/app.css', 'css/swiper-bundle.min.css'];
        $css_exist = 0;
        foreach ($css_files as $file) {
            if (file_exists(__DIR__ . '/' . $file)) $css_exist++;
        }
        if ($css_exist === count($css_files)) {
            $checks[] = ['name' => 'CSS Files', 'status' => 'pass', 'message' => 'All required CSS files exist'];
        } else {
            $checks[] = ['name' => 'CSS Files', 'status' => 'fail', 'message' => $css_exist . '/' . count($css_files) . ' CSS files found'];
        }

        // Check 6: JS files
        $js_files = ['js/jquery.min.js', 'js/app.js', 'js/bootstrap.min.js'];
        $js_exist = 0;
        foreach ($js_files as $file) {
            if (file_exists(__DIR__ . '/' . $file)) $js_exist++;
        }
        if ($js_exist === count($js_files)) {
            $checks[] = ['name' => 'JS Files', 'status' => 'pass', 'message' => 'All required JS files exist'];
        } else {
            $checks[] = ['name' => 'JS Files', 'status' => 'fail', 'message' => $js_exist . '/' . count($js_files) . ' JS files found'];
        }

        // Display results
        foreach ($checks as $check) {
            echo '<div class="check ' . $check['status'] . '">';
            echo '<strong>' . $check['name'] . ':</strong> ' . $check['message'];
            echo '</div>';
        }

        // Count failures
        $failures = array_filter($checks, function($c) { return $c['status'] === 'fail'; });
        if (count($failures) > 0) {
            echo '<div class="solution">';
            echo '<h3>❌ Issues Found</h3>';
            echo '<p><strong>Most Common Issue:</strong> The <code>menu.json</code> file was not uploaded to your server.</p>';
            echo '<h4>📋 Solution Checklist:</h4>';
            echo '<ol>';
            echo '<li>Make sure you upload ALL files from the <code>lounge/</code> folder to your server</li>';
            echo '<li>Specifically ensure <code>menu.json</code> is in the same directory as <code>index.php</code></li>';
            echo '<li>Check file permissions - web server must be able to read menu.json (usually 644 or 755)</li>';
            echo '<li>If using FTP, verify binary mode for JSON</li>';
            echo '<li>After uploading, refresh this page to confirm</li>';
            echo '</ol>';
            echo '</div>';
        } else {
            echo '<div class="solution" style="background: #d4edda; border-left-color: #28a745;">';
            echo '<h3>✅ All Checks Passed!</h3>';
            echo '<p>Your menu files are set up correctly. <a href="index.php">Go to Lounge Menu →</a></p>';
            echo '</div>';
        }
        ?>
    </div>
</body>
</html>
