<?php
// Test script to simulate exam submission
require_once 'config/database.php';

// Simulate a student submitting answers
$online_exam_id = 1;
$student_id = 5642; // Valid student ID from our check

// Sample answers for questions 14, 15, 16, 17, 18
$answers = [
    14 => 'A',
    15 => 'B',
    16 => 'C',
    17 => 'D',
    18 => 'A'
];

try {
    foreach ($answers as $question_id => $answer) {
        echo "Processing answer for question $question_id: $answer\n";
        
        // Get question details
        $stmt = $pdo->prepare("SELECT * FROM online_exam_questions WHERE id = ? AND online_exam_id = ?");
        $stmt->execute([$question_id, $online_exam_id]);
        $question = $stmt->fetch();
        
        if (!$question) {
            echo "Question $question_id not found\n";
            continue;
        }
        
        echo "Question found: " . substr($question['question_text'], 0, 30) . "...\n";
        
        // Calculate marks
        $marks_obtained = 0;
        if ($question['question_type'] === 'mcq' && $answer === $question['correct_answer']) {
            $marks_obtained = $question['marks'];
        }
        
        // Determine if it's a selected option or written answer
        $selected_option = null;
        $written_answer = null;
        
        if ($question['question_type'] === 'mcq') {
            $selected_option = $answer;
        } else {
            $written_answer = is_string($answer) ? $answer : '';
        }
        
        echo "Saving response: exam_id=$online_exam_id, student_id=$student_id, question_id=$question_id, option=$selected_option, marks=$marks_obtained\n";
        
        // Save response to database
        $stmt = $pdo->prepare("INSERT INTO online_exam_student_responses (online_exam_id, student_id, question_id, selected_option, written_answer, marks_obtained) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE selected_option = VALUES(selected_option), written_answer = VALUES(written_answer), marks_obtained = VALUES(marks_obtained)");
        $result = $stmt->execute([$online_exam_id, $student_id, $question_id, $selected_option, $written_answer, $marks_obtained]);
        
        if ($result) {
            echo "Response saved successfully for question $question_id\n";
        } else {
            echo "Failed to save response for question $question_id\n";
        }
        echo "\n";
    }
    
    echo "Test submission completed!\n";
    
    // Now check if we can retrieve the responses
    echo "\nChecking saved responses:\n";
    $stmt = $pdo->prepare("SELECT sr.*, q.question_text FROM online_exam_student_responses sr JOIN online_exam_questions q ON sr.question_id = q.id WHERE sr.online_exam_id = ? AND sr.student_id = ?");
    $stmt->execute([$online_exam_id, $student_id]);
    $responses = $stmt->fetchAll();
    
    foreach ($responses as $response) {
        echo "Question: " . substr($response['question_text'], 0, 30) . "... | Answer: " . ($response['selected_option'] ?? $response['written_answer']) . " | Marks: " . $response['marks_obtained'] . "\n";
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>