<?php
session_start();
if (!isset($_SESSION['admin'])) {
    header('Location: login.php');
    exit();
}
include 'db.php';

$quiz_id = isset($_GET['quiz_id']) ? (int)$_GET['quiz_id'] : 0;

$quizName = "Global Results";
if ($quiz_id) {
    $qStmt = $conn->prepare("SELECT quiz_name FROM quiz_games WHERE quiz_id = ?");
    $qStmt->bind_param("i", $quiz_id);
    $qStmt->execute();
    $quizName = $qStmt->get_result()->fetch_assoc()['quiz_name'] ?? "Quiz Results";
}

// Fetch Leaderboard
$query = "SELECT ql.*, u.Username, qg.quiz_name 
          FROM quiz_leaderboard ql 
          JOIN users u ON ql.user_id = u.UserID 
          JOIN quiz_games qg ON ql.quiz_id = qg.quiz_id";

if ($quiz_id) $query .= " WHERE ql.quiz_id = $quiz_id";
$query .= " ORDER BY ql.score DESC LIMIT 50";

$results = $conn->query($query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quiz Analytics - <?php echo $quizName; ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body { background-color: #f0f4f8; }
        .sidebar { height: 100vh; background: #2c3e50; color: white; padding-top: 20px; position: fixed; width: 250px; }
        .sidebar a { color: #bdc3c7; text-decoration: none; display: block; padding: 10px 20px; }
        .sidebar a:hover, .sidebar a.active { background: #34495e; color: white; }
        .main-content { margin-left: 250px; padding: 40px; }
        .card { border: none; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
    </style>
</head>
<body>
    <div class="sidebar">
        <h3 class="text-center mb-4">GameHub Admin</h3>
        <a href="admin.php"><i class="fas fa-home me-2"></i> Dashboard</a>
        <a href="games.php"><i class="fas fa-gamepad me-2"></i> Games</a>
        <a href="manage_quizzes.php" class="active"><i class="fas fa-lightbulb me-2"></i> Quizzes</a>
        <a href="logout.php"><i class="fas fa-sign-out-alt me-2"></i> Logout</a>
    </div>

    <div class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="manage_quizzes.php">Quizzes</a></li>
                        <li class="breadcrumb-item active"><?php echo htmlspecialchars($quizName); ?></li>
                    </ol>
                </nav>
                <h2>Performance Analytics</h2>
            </div>
        </div>

        <div class="card">
            <div class="card-header bg-white py-3">
                <h5 class="mb-0 fw-bold"><i class="fas fa-trophy text-warning me-2"></i> Top Performers</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover align-middle">
                        <thead class="table-light">
                            <tr>
                                <th>Rank</th>
                                <th>User</th>
                                <th>Quiz</th>
                                <th>Score</th>
                                <th>XP Earned</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($results && $results->num_rows > 0): ?>
                                <?php $rank = 1; while($row = $results->fetch_assoc()): ?>
                                    <tr>
                                        <td><strong>#<?php echo $rank++; ?></strong></td>
                                        <td><?php echo htmlspecialchars($row['Username']); ?></td>
                                        <td><span class="badge bg-info text-dark"><?php echo htmlspecialchars($row['quiz_name']); ?></span></td>
                                        <td><span class="fw-bold text-primary"><?php echo $row['score']; ?></span></td>
                                        <td><span class="text-success">+<?php echo $row['experience_points']; ?> XP</span></td>
                                        <td class="text-muted small"><?php echo $row['updated_at'] ?? $row['created_at'] ?? 'N/A'; ?></td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr><td colspan="6" class="text-center py-4">No results recorded yet for this quiz.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
