<?php
// FILE PATH: C:\xampp\htdocs\Project\student\cbt_lobby.php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require_once '../config/database.php';

// 1. ACCESS CONTROL
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'student') {
    header("Location: ../auth/login.php"); 
    exit();
}

$student_id = (int)$_SESSION['user_id'];

try {
    // 2. FETCH STUDENT DATA & ACCESS STATUS
    $stmt = $pdo->prepare("SELECT fullname, class_id, result_access FROM students WHERE id = ?");
    $stmt->execute([$student_id]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$user) { die("Profile not found."); }

    // --- FEE RESTRICTION GATEKEEPER ---
    if ($user['result_access'] == 0) {
        ?>
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Access Restricted</title>
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
            <style>
                body { margin: 0; font-family: 'Plus Jakarta Sans', sans-serif; background: #f8fafc; }
                .restriction-overlay {
                    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
                    background: rgba(15, 23, 42, 0.9);
                    display: flex; justify-content: center; align-items: center;
                    z-index: 9999; padding: 20px; box-sizing: border-box;
                }
                .restriction-card {
                    background: white; color: #333;
                    padding: 40px; border-radius: 20px;
                    max-width: 450px; width: 100%;
                    box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1);
                    text-align: center;
                }
                .restriction-card i { color: #ef4444; font-size: 60px; margin-bottom: 20px; }
                .restriction-card h2 { margin: 0 0 15px 0; color: #1e293b; font-size: 24px; font-weight: 800; }
                .restriction-card p { color: #64748b; line-height: 1.6; font-size: 16px; }
                .btn-back {
                    display: inline-block; margin-top: 25px;
                    padding: 12px 30px; background: #2563eb;
                    color: white; text-decoration: none; border-radius: 12px;
                    font-weight: 700; transition: 0.2s;
                }
                .btn-back:hover { background: #1d4ed8; transform: translateY(-2px); }
            </style>
        </head>
        <body>
            <div class="restriction-overlay">
                <div class="restriction-card">
                    <i class="fa fa-circle-exclamation"></i>
                    <h2>Account Restricted</h2>
                    <p>Your access to the Computer Based Test (CBT) portal has been suspended due to <strong>outstanding school fees</strong>.</p>
                    <p>Please contact the school bursary or administrative office to resolve this and regain access.</p>
                    <a href="dashboard.php" class="btn-back"><i class="fa fa-arrow-left"></i> Back to Dashboard</a>
                </div>
            </div>
        </body>
        </html>
        <?php
        exit(); // STOP ALL FURTHER PROCESSING
    }

    $class_id = (int)$user['class_id'];

    // 3. FETCH SYSTEM SETTINGS
    $stmtSet = $pdo->query("SELECT active_session, active_term FROM system_settings WHERE id = 1");
    $settings = $stmtSet->fetch(PDO::FETCH_ASSOC);

    $sys_session = trim($settings['active_session'] ?? '');
    $sys_term    = trim($settings['active_term'] ?? '');

    // 4. FETCH ELIGIBLE EXAMS
    $sql = "SELECT e.*, 
            (SELECT COUNT(*) FROM questions WHERE exam_id = e.id) as q_count
            FROM exams e 
            WHERE e.class_id = ? 
            AND e.status = 1 
            AND e.is_deleted = 0
            AND UPPER(TRIM(e.session)) = UPPER(TRIM(?))
            AND UPPER(TRIM(e.term)) = UPPER(TRIM(?))
            AND e.id NOT IN (SELECT exam_id FROM exam_results WHERE student_id = ?)
            HAVING q_count > 0";

    $stmtExams = $pdo->prepare($sql);
    $stmtExams->execute([$class_id, $sys_session, $sys_term, $student_id]);
    $exams = $stmtExams->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    die("Database Error: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exam Lobby | <?= htmlspecialchars($user['fullname']) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root { --primary: #2563eb; --bg: #f8fafc; --text: #0f172a; }
        body { font-family: 'Plus Jakarta Sans', sans-serif; background: var(--bg); color: var(--text); margin: 0; padding: 20px; }
        .container { max-width: 900px; margin: 40px auto; }
        .header { margin-bottom: 35px; }
        .btn-back { text-decoration: none; color: #64748b; font-weight: 700; font-size: 14px; display: inline-flex; align-items: center; gap: 8px; margin-bottom: 10px; }
        
        .exam-card { 
            background: white; padding: 25px; border-radius: 20px; border: 1px solid #e2e8f0; 
            display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;
            transition: 0.2s ease-in-out;
        }
        .exam-card:hover { border-color: var(--primary); transform: translateY(-2px); box-shadow: 0 10px 15px -3px rgba(0,0,0,0.05); }
        
        .exam-title { margin: 0; font-size: 19px; font-weight: 800; color: #1e293b; }
        .meta { display: flex; gap: 12px; margin-top: 10px; }
        .tag { background: #f1f5f9; color: #475569; padding: 5px 12px; border-radius: 8px; font-size: 12px; font-weight: 700; }
        
        .btn-start { 
            background: var(--primary); color: white; padding: 12px 24px; border-radius: 12px; 
            text-decoration: none; font-weight: 800; font-size: 14px; transition: 0.2s;
        }
        .btn-start:hover { background: #1d4ed8; }

        .empty { text-align: center; padding: 60px; background: white; border-radius: 20px; border: 2px dashed #e2e8f0; }
    </style>
</head>
<body>

<div class="container">
    <div class="header">
        <a href="dashboard.php" class="btn-back"><i class="fa fa-arrow-left"></i> Dashboard</a>
        <h1>Available Exams</h1>
        <p style="color: #64748b;">Session: <?= htmlspecialchars($sys_session) ?> | Term: <?= htmlspecialchars($sys_term) ?></p>
    </div>

    <?php if(empty($exams)): ?>
        <div class="empty">
            <i class="fa fa-clipboard-question" style="font-size: 40px; color: #cbd5e1; margin-bottom: 15px;"></i>
            <h3>No Exams Found</h3>
            <p style="color: #64748b;">There are currently no active exams for your class in this session/term.</p>
        </div>
    <?php else: ?>
        <?php foreach($exams as $exam): ?>
            <div class="exam-card">
                <div>
                    <h2 class="exam-title"><?= htmlspecialchars($exam['exam_title']) ?></h2>
                    <div class="meta">
                        <span class="tag"><i class="fa fa-clock"></i> <?= (int)$exam['duration'] ?> Mins</span>
                        <span class="tag"><i class="fa fa-list"></i> <?= $exam['q_count'] ?> Questions</span>
                    </div>
                </div>
                <a href="take_exam.php?exam_id=<?= (int)$exam['id'] ?>" class="btn-start" 
                   onclick="return confirm('Do you want to start this exam? The timer begins immediately.')">
                    Start Exam <i class="fa fa-play" style="margin-left:5px;"></i>
                </a>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

</body>
</html>