<?php
include '../../includes/conn.php';
include '../includes/header.php';
requireRole('teacher');

$teacher_id = $_SESSION['user_id'];

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

$exam_id = intval($_GET['exam_id']);

// Verify the exam belongs to the logged-in teacher
$exam_stmt = $pdo->prepare("SELECT * FROM cbt_exams WHERE id = ? AND teacher_id = ?");
$exam_stmt->execute([$exam_id, $teacher_id]);
$exam = $exam_stmt->fetch(PDO::FETCH_ASSOC);

if (!$exam) {
    die("Invalid exam or access denied.");
}

// Fetch all student results for this exam
$results_stmt = $pdo->prepare("
    SELECT 
        r.id,
        u.first_name,
        u.last_name,
        r.total_possible,
        r.correct_option AS correct_answers,
        r.score,
        r.submitted_at
    FROM cbt_exam_results r
    INNER JOIN users u ON r.student_id = u.id
    WHERE r.exam_id = ?
    ORDER BY r.score DESC
");
$results_stmt->execute([$exam_id]);
$results = $results_stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Exam Details - <?= htmlspecialchars($exam['title']) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3>Exam Report: <?= htmlspecialchars($exam['title']) ?></h3>
        <a href="exam_report.php" class="btn btn-secondary btn-sm">← Back to Reports</a>
    </div>

    <?php if (empty($results)): ?>
        <div class="alert alert-info">No students have taken this exam yet.</div>
    <?php else: ?>
        <div class="card shadow-sm">
            <div class="card-body">
                <table class="table table-bordered table-striped">
                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th>Student Name</th>
                            <th>Correct Answers</th>
                            <th>Total Questions</th>
                            <th>Score (%)</th>
                            <th>Submitted At</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $sn = 1;
                        foreach ($results as $r): 
                        ?>
                            <tr>
                                <td><?= $sn++ ?></td>
                                <td><?= htmlspecialchars($r['first_name'] . ' ' . $r['last_name']) ?></td>
                                <td><?= $r['correct_answers'] ?></td>
                                <td><?= $r['total_possible'] ?></td>
                                <td><strong><?= $r['score'] ?>%</strong></td>
                                <td><?= $r['submitted_at'] ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    <?php endif; ?>
</div>
</body>
</html>
