<?php
include '../../includes/conn.php';

include '../includes/header.php';
requireRole('student');

$student_id = $_SESSION['user_id'];

// Check if exam ID is provided
if (!isset($_GET['exam_id'])) {
    die("Exam ID not provided.");
}
$exam_id = intval($_GET['exam_id']);

// Fetch exam info
$exam_stmt = $pdo->prepare("
    SELECT e.*, c.name AS class_name
    FROM cbt_exams e
    JOIN classrooms c ON e.classroom_id = c.id
    WHERE e.id = ?
");
$exam_stmt->execute([$exam_id]);
$exam = $exam_stmt->fetch(PDO::FETCH_ASSOC);

if (!$exam) {
    die("Exam not found.");
}

// Fetch student's result
$result_stmt = $pdo->prepare("
    SELECT * FROM cbt_exam_results 
    WHERE exam_id = ? AND student_id = ?
");
$result_stmt->execute([$exam_id, $student_id]);
$result = $result_stmt->fetch(PDO::FETCH_ASSOC);

if (!$result) {
    echo "<script>alert('You have not taken this exam yet.'); window.location.href='available_exams.php';</script>";
    exit;
}

// Fetch student's answers
$answers_stmt = $pdo->prepare("
    SELECT a.*, q.question_text, q.option_a, q.option_b, q.option_c, q.option_d, q.correct_option
    FROM cbt_answers a
    JOIN cbt_questions q ON a.question_id = q.id
    WHERE a.exam_id = ? AND a.student_id = ?
");
$answers_stmt->execute([$exam_id, $student_id]);
$answers = $answers_stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Review: <?= htmlspecialchars($exam['title']) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .correct { color: #28a745; font-weight: bold; }
        .wrong { color: #dc3545; font-weight: bold; }
        .answer-box {
            border-left: 5px solid #dee2e6;
            padding: 15px;
            margin-bottom: 15px;
            background-color: #f8f9fa;
            border-radius: 10px;
        }
    </style>
</head>
<body class="bg-light">
<div class="container mt-5">
    <div class="card shadow">
        <div class="card-header bg-primary text-white">
            <h4><?= htmlspecialchars($exam['title']) ?> - Review</h4>
        </div>
        <div class="card-body">
            <div class="mb-3">
                <p><strong>Class:</strong> <?= htmlspecialchars($exam['class_name']) ?></p>
                <p><strong>Score:</strong> <?= $result['score'] ?>%</p>
                <p><strong>Correct Answers:</strong> <?= $result['correct_option'] ?> / <?= $result['total_possible'] ?></p>
                <p><strong>Date Submitted:</strong> <?= date('d M Y, h:i A', strtotime($result['submitted_at'])) ?></p>
            </div>

            <hr>

            <?php if (empty($answers)): ?>
                <div class="alert alert-info">No answer records found for this exam.</div>
            <?php else: ?>
                <?php foreach ($answers as $index => $a): ?>
                    <?php 
                        $is_correct = ($a['selected_option'] === $a['correct_option']);
                        $class = $is_correct ? 'correct' : 'wrong';
                    ?>
                    <div class="answer-box <?= $class ?>">
                        <h6>Q<?= $index + 1 ?>. <?= htmlspecialchars($a['question_text']) ?></h6>
                        <p><strong>Your answer:</strong> 
                            <?= $a['selected_option'] ? htmlspecialchars($a['selected_option']) : 'No answer' ?>
                        </p>
                        <p><strong>Correct answer:</strong> <?= htmlspecialchars($a['correct_option']) ?></p>
                        <?php if ($is_correct): ?>
                            <span class="badge bg-success">Correct</span>
                        <?php else: ?>
                            <span class="badge bg-danger">Wrong</span>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>

            <div class="text-center mt-4">
                <a href="available_exams.php" class="btn btn-secondary">Back to Exams</a>
            </div>
        </div>
    </div>
</div>
</body>
</html>
