<?php
include 'db.php';

$category_id = $_GET['category_id'];
$topic_id = $_GET['topic_id'];

$sql = "
    SELECT 
        u.id AS user_id,
        u.name AS user_name,
        u.email,
        c.name AS category_name,
        t.name AS topic_name,
        uq.score,
        uq.attempt_time
    FROM 
        users u
    LEFT JOIN 
        categories c ON u.category_id = c.id
    LEFT JOIN 
        topics t ON t.category_id = c.id
    LEFT JOIN 
        user_quizzes uq ON uq.user_id = u.id AND uq.topic_id = t.id
    WHERE 
        c.id = ? AND t.id = ?
    ORDER BY 
        u.name
";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $category_id, $topic_id);
$stmt->execute();
$result = $stmt->get_result();

$users = array();
while ($row = $result->fetch_assoc()) {
    $users[] = $row;
}
echo json_encode($users);

$conn->close();
?>
