<?php
include 'db.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $user_id = $_POST['user_id'];

    // Fetch all topics the user has completed
  $stmt = $pdo->prepare("
    SELECT t.name as topic_name, uq.score, 
           (SELECT COUNT(q.id) FROM questions q WHERE q.topic_id = t.id) as total_questions 
    FROM user_quizzes uq
    JOIN topics t ON uq.topic_id = t.id
    WHERE uq.user_id = ?
");


    $stmt->execute([$user_id]);
    $topics = $stmt->fetchAll(PDO::FETCH_ASSOC);

   if ($topics) {
    echo json_encode([
        'status' => 'success',
        'topics' => $topics
    ]);
} else {
    echo json_encode([
        'status' => 'error',
        'message' => 'No completed topics found.'
    ]);
}

}
?>
