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

$student_id = $_SESSION['user_id'];

// Fetch the classroom of the logged-in student
$class_stmt = $pdo->prepare("
    SELECT classroom_id 
    FROM classroom_students 
    WHERE student_id = ?
");
$class_stmt->execute([$student_id]);
$class_info = $class_stmt->fetch(PDO::FETCH_ASSOC);

if (!$class_info) {
    die("You are not assigned to any classroom. Please contact your teacher or admin.");
}

$classroom_id = $class_info['classroom_id'];

// Fetch only active exams for this student's classroom
$exams_stmt = $pdo->prepare("
    SELECT e.id, e.title, e.description, e.exam_date, e.duration, e.status,
           c.name AS class_name, s.name AS session_name, t.name AS term_name
    FROM cbt_exams e
    JOIN classrooms c ON e.classroom_id = c.id
    JOIN sessions s ON e.session_id = s.id
    JOIN terms t ON e.term_id = t.id
    WHERE e.status = 'active' 
      AND e.classroom_id = ?
    ORDER BY e.exam_date DESC
");
$exams_stmt->execute([$classroom_id]);
$exams = $exams_stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Available Exams</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="card shadow-sm">
        <div class="card-header bg-primary text-white">
            <h4 class="mb-0">Available Exams</h4>
        </div>
        <div class="card-body">
            <?php if (empty($exams)): ?>
                <div class="alert alert-info">No active exams available for your class right now.</div>
            <?php else: ?>
                <table class="table table-bordered table-striped align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>Title</th>
                            <th>Class</th>
                            <th>Session</th>
                            <th>Term</th>
                            <th>Date</th>
                            <th>Duration</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($exams as $exam): ?>
                        <tr>
                            <td><?= htmlspecialchars($exam['title']) ?></td>
                            <td><?= htmlspecialchars($exam['class_name']) ?></td>
                            <td><?= htmlspecialchars($exam['session_name']) ?></td>
                            <td><?= htmlspecialchars($exam['term_name']) ?></td>
                            <td><?= htmlspecialchars($exam['exam_date']) ?></td>
                            <td><?= htmlspecialchars($exam['duration']) ?> mins</td>
                            <td><span class="badge bg-success"><?= ucfirst($exam['status']) ?></span></td>
                            <td>
                                <a href="take_exam.php?exam_id=<?= $exam['id'] ?>" class="btn btn-primary btn-sm">
                                    Start Exam
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>
</div>
</body>
</html>
