<?php
session_start();
$conn = new mysqli("localhost", "root", "", "breakout_db");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$lb_easy = $conn->query("SELECT username, highscore_easy FROM users WHERE highscore_easy > 0 ORDER BY highscore_easy DESC LIMIT 10");

$lb_medium = $conn->query("SELECT username, highscore_medium FROM users WHERE highscore_medium > 0 ORDER BY highscore_medium DESC LIMIT 10");

$lb_hard = $conn->query("SELECT username, highscore_hard FROM users WHERE highscore_hard > 0 ORDER BY highscore_hard DESC LIMIT 10");

$isLoggedIn = isset($_SESSION['user']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Leaderboard - Breakout Game</title>

    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

    <link rel="stylesheet" href="style.css">
</head>
<body class="leaderboard-body">

    <h2><i class="fas fa-trophy"></i> Hall of Fame</h2>

    <div class="menu">
        <a href="index.php"><i class="fas fa-home"></i> Home</a>

        <?php if($isLoggedIn): ?>
            <a href="logout.php"><i class="fas fa-right-from-bracket"></i> Logout</a>
        <?php else: ?>
            <a href="login.php"><i class="fas fa-sign-in-alt"></i> Login</a>
        <?php endif; ?>
    </div>

    <div class="leaderboard-wrapper">
        
        <div class="lb-column">
            <h3 class="easy-title">Easy Level</h3>
            <table>
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Score</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    if ($lb_easy->num_rows > 0) {
                        while($r = $lb_easy->fetch_assoc()) { 
                            echo "<tr>
                                    <td>" . htmlspecialchars($r['username']) . "</td>
                                    <td>" . htmlspecialchars($r['highscore_easy']) . "</td>
                                  </tr>";
                        }
                    } else {
                        echo "<tr><td colspan='2' style='text-align:center;'>No records yet</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>

        <div class="lb-column">
            <h3 class="medium-title">Medium Level</h3>
            <table>
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Score</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    if ($lb_medium->num_rows > 0) {
                        while($r = $lb_medium->fetch_assoc()) { 
                            echo "<tr>
                                    <td>" . htmlspecialchars($r['username']) . "</td>
                                    <td>" . htmlspecialchars($r['highscore_medium']) . "</td>
                                  </tr>";
                        }
                    } else {
                        echo "<tr><td colspan='2' style='text-align:center;'>No records yet</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>

        <div class="lb-column">
            <h3 class="hard-title">Hard Level</h3>
            <table>
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Score</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    if ($lb_hard->num_rows > 0) {
                        while($r = $lb_hard->fetch_assoc()) { 
                            echo "<tr>
                                    <td>" . htmlspecialchars($r['username']) . "</td>
                                    <td>" . htmlspecialchars($r['highscore_hard']) . "</td>
                                  </tr>";
                        }
                    } else {
                        echo "<tr><td colspan='2' style='text-align:center;'>No records yet</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>

    </div>
    <a href="index.php" class="back-button">⬅ Back to Game</a>

</body>
</html>