<?php
// Full test of both question order and option randomization
require_once 'config/database.php';

echo "=== Full Randomization Test ===\n\n";

try {
    // Simulate the exam page logic
    $online_exam_id = 1;
    
    // Fetch exam questions (same as in take_exam.php)
    $stmt = $pdo->prepare("SELECT * FROM online_exam_questions WHERE online_exam_id = ? ORDER BY RAND()");
    $stmt->execute([$online_exam_id]);
    $questions = $stmt->fetchAll();
    
    echo "Questions fetched (should be in random order):\n";
    foreach ($questions as $index => $question) {
        echo ($index + 1) . ". " . substr($question['question_text'], 0, 50) . "...\n";
    }
    
    echo "\nTesting option shuffling for MCQ questions:\n";
    
    // Add function to shuffle options (same as in take_exam.php)
    function get_shuffled_options($question) {
        if ($question['question_type'] !== 'mcq') {
            return $question;
        }
        
        // Create an array of options with their values
        $options = [
            'A' => $question['option_a'],
            'B' => $question['option_b'],
            'C' => $question['option_c'],
            'D' => $question['option_d']
        ];
        
        // Store original correct answer value
        $original_correct_value = $options[$question['correct_answer']];
        
        // Convert to array of values and shuffle
        $option_values = array_values($options);
        shuffle($option_values);
        
        // Map shuffled values back to labels
        $shuffled_options = [
            'option_a' => $option_values[0],
            'option_b' => $option_values[1],
            'option_c' => $option_values[2],
            'option_d' => $option_values[3]
        ];
        
        // Find the new correct answer label
        $new_correct_answer = '';
        foreach (['A', 'B', 'C', 'D'] as $i => $label) {
            if ($option_values[$i] === $original_correct_value) {
                $new_correct_answer = $label;
                break;
            }
        }
        
        // Update the question with shuffled options
        $question['option_a'] = $shuffled_options['option_a'];
        $question['option_b'] = $shuffled_options['option_b'];
        $question['option_c'] = $shuffled_options['option_c'];
        $question['option_d'] = $shuffled_options['option_d'];
        $question['correct_answer'] = $new_correct_answer;
        
        return $question;
    }
    
    // Process questions to randomize options
    foreach ($questions as &$question) {
        $question = get_shuffled_options($question);
    }
    unset($question); // Break the reference with the last element
    
    echo "\nSample of shuffled questions with options:\n";
    foreach (array_slice($questions, 0, 3) as $index => $question) {
        if ($question['question_type'] === 'mcq') {
            echo "\nQuestion " . ($index + 1) . ": " . substr($question['question_text'], 0, 50) . "...\n";
            echo "  A: " . $question['option_a'] . "\n";
            echo "  B: " . $question['option_b'] . "\n";
            echo "  C: " . $question['option_c'] . "\n";
            echo "  D: " . $question['option_d'] . "\n";
            echo "  Correct answer: " . $question['correct_answer'] . "\n";
        }
    }
    
    echo "\n✓ Full randomization test completed successfully\n";
    echo "Both question order and options are now randomized for each exam attempt.\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>