<?php
$pageTitle = "Admin - Exam Details";
include '../../includes/conn.php';

// Include header
include '../includes/header.php';

requireRole('admin'); // only admins can access

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.title, c.name AS class_name, s.name AS session_name, t.name AS term_name,
           CONCAT(u.first_name,' ',u.last_name) AS teacher_name
    FROM cbt_exams e
    LEFT JOIN classrooms c ON e.classroom_id = c.id
    LEFT JOIN sessions s ON e.session_id = s.id
    LEFT JOIN terms t ON e.term_id = t.id
    LEFT JOIN users u ON e.teacher_id = u.id
    WHERE e.id = ?
");
$exam_stmt->execute([$exam_id]);
$exam = $exam_stmt->fetch(PDO::FETCH_ASSOC);

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

// Fetch questions
$questions_stmt = $pdo->prepare("
    SELECT id, question_text, option_a, option_b, option_c, option_d, correct_option
    FROM cbt_questions
    WHERE exam_id = ?
");
$questions_stmt->execute([$exam_id]);
$questions = $questions_stmt->fetchAll(PDO::FETCH_ASSOC);

// Fetch student results
$results_stmt = $pdo->prepare("
    SELECT u.id AS student_id, u.first_name , u.last_name AS student_name, r.score, r.correct_option, r.total_possible
    FROM cbt_exam_results r
    LEFT 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);

// Statistics
$total_attempts = count($results);
$avg_score = $total_attempts ? round(array_sum(array_column($results,'score'))/$total_attempts,2) : 0;
$max_score = $total_attempts ? max(array_column($results,'score')) : 0;
$min_score = $total_attempts ? min(array_column($results,'score')) : 0;
?>

<div class="container mt-4">
    <h2>Exam Details: <?= htmlspecialchars($exam['title']) ?></h2>
    <p>
        <strong>Class:</strong> <?= htmlspecialchars($exam['class_name']) ?> | 
        <strong>Session:</strong> <?= htmlspecialchars($exam['session_name']) ?> | 
        <strong>Term:</strong> <?= htmlspecialchars($exam['term_name']) ?> | 
        <strong>Teacher:</strong> <?= htmlspecialchars($exam['teacher_name']) ?>
    </p>

    <div class="row mb-4">
        <div class="col-md-4"><strong>Total Attempts:</strong> <?= $total_attempts ?></div>
        <div class="col-md-4"><strong>Average Score:</strong> <?= $avg_score ?>%</div>
        <div class="col-md-4"><strong>Score Range:</strong> <?= $min_score ?>% - <?= $max_score ?>%</div>
    </div>

    <!-- Questions Table -->
    <h4>Questions</h4>
    <?php if(empty($questions)): ?>
        <div class="alert alert-info">No questions added for this exam yet.</div>
    <?php else: ?>
        <table class="table table-bordered table-striped">
            <thead class="table-dark">
                <tr>
                    <th>#</th>
                    <th>Question</th>
                    <th>Option A</th>
                    <th>Option B</th>
                    <th>Option C</th>
                    <th>Option D</th>
                    <th>Correct Option</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($questions as $index => $q): ?>
                    <tr>
                        <td><?= $index+1 ?></td>
                        <td><?= htmlspecialchars($q['question_text']) ?></td>
                        <td><?= htmlspecialchars($q['option_a']) ?></td>
                        <td><?= htmlspecialchars($q['option_b']) ?></td>
                        <td><?= htmlspecialchars($q['option_c']) ?></td>
                        <td><?= htmlspecialchars($q['option_d']) ?></td>
                        <td><?= strtoupper($q['correct_option']) ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

    <!-- Student Results Table -->
    <h4 class="mt-4">Student Results</h4>
    <?php if(empty($results)): ?>
        <div class="alert alert-info">No student has attempted this exam yet.</div>
    <?php else: ?>
        <table class="table table-bordered table-striped">
            <thead class="table-dark">
                <tr>
                    <th>#</th>
                    <th>Student Name</th>
                    <th>Score (%)</th>
                    <th>Correct Answers</th>
                    <th>Total Questions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($results as $index => $r): ?>
                    <tr>
                        <td><?= $index+1 ?></td>
                        <td><?= htmlspecialchars($r['student_name']) ?></td>
                        <td><?= $r['score'] ?></td>
                        <td><?= $r['correct_option'] ?></td>
                        <td><?= $r['total_possible'] ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<?php include '../includes/footer.php'; ?>
