<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/student-auth.php';
$pdo=db();requireStudent();$student=currentStudent($pdo);if(!$student){studentLogout();redirect('login.php');}
$examId=(int)($_GET['id']??$_GET['exam_id']??0);
$exam=$examId>0?safeFetch($pdo,'SELECT id,course_id FROM core_exams WHERE id=? LIMIT 1',[$examId]):null;
if(!$exam){flash('error','Exam not found.');redirect('student/exams.php');}
$enrolled=safeFetch($pdo,"SELECT id FROM core_course_students WHERE student_id=? AND course_id=? AND status IN ('active','approved','completed') LIMIT 1",[(int)$student['id'],(int)$exam['course_id']]);
if(!$enrolled){http_response_code(403);exit('You are not enrolled in this course.');}
redirect('student/exams.php?view=lobby&course_id='.(int)$exam['course_id'].'&exam_id='.(int)$exam['id']);
