DELIMITER //
CREATE PROCEDURE GetUnansweredQuestions(IN p_user_id INT, IN p_group_id INT)
BEGIN
    SELECT q.*
    FROM Questions q
    LEFT JOIN UserQuestions uq ON q.question_id = uq.question_id AND uq.user_id = p_user_id
    WHERE uq.question_id IS NULL
    ORDER BY q.difficulty ASC
    LIMIT 5;
END //
DELIMITER ;

DELIMITER //
CREATE PROCEDURE UpdateUserScore(IN p_user_id INT, IN p_correct_answers INT)
BEGIN
    DECLARE base_score FLOAT;
    SET base_score = IF(p_correct_answers >= 3, 1, p_correct_answers * 0.333);
    
    INSERT INTO UserScores (user_id, total_score) 
    VALUES (p_user_id, base_score) 
    ON DUPLICATE KEY UPDATE total_score = total_score + base_score;
END //
DELIMITER ;
END //
DELIMITER ;

switch ($command) {
    case 'update_score':
        updateUserScore($pdo, $_GET['user_id'], $_GET['correct_answers']);
        break;
        break;
        break;
    echo json_encode(["status" => "success", "message" => "Score updated"]);
    echo json_encode(["status" => "success", "message" => "Chat message logged"]);