<?php
// Test script to verify question and option randomization
require_once 'config/database.php';

echo "=== Testing Question and Option Randomization ===\n\n";

try {
    // Get questions for online exam ID 1
    $online_exam_id = 1;
    $stmt = $pdo->prepare("SELECT * FROM online_exam_questions WHERE online_exam_id = ? ORDER BY RAND() LIMIT 1");
    $stmt->execute([$online_exam_id]);
    $question = $stmt->fetch();
    
    if ($question) {
        echo "Original question:\n";
        echo "Question: " . substr($question['question_text'], 0, 50) . "...\n";
        echo "Options:\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\n";
        
        // Test our shuffle function
        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;
        }
        
        // Test multiple shuffles
        echo "Shuffled versions:\n";
        for ($i = 1; $i <= 5; $i++) {
            $shuffled = get_shuffled_options($question);
            echo "Shuffle $i:\n";
            echo "  A: " . $shuffled['option_a'] . "\n";
            echo "  B: " . $shuffled['option_b'] . "\n";
            echo "  C: " . $shuffled['option_c'] . "\n";
            echo "  D: " . $shuffled['option_d'] . "\n";
            echo "  Correct answer: " . $shuffled['correct_answer'] . "\n\n";
        }
        
        echo "✓ Randomization test completed successfully\n";
    } else {
        echo "No questions found for exam ID $online_exam_id\n";
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>