<?php
include 'db.php';

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

$sql = "
    SELECT 
        q.id AS question_id,
        q.text AS question_text,
        o.id AS option_id,
        o.text AS option_text,
        ua.selected_option_id
    FROM 
        questions q
    LEFT JOIN 
        options o ON o.question_id = q.id
    LEFT JOIN 
        user_answers ua ON ua.question_id = q.id AND ua.user_id = ? AND ua.topic_id = ?
    WHERE 
        q.topic_id = ?
    ORDER BY 
        q.id
";

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

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

$conn->close();
?>
