<?php
// Debugging (Remove in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);

session_start();

// 1. Check Database Connection
if (!file_exists('db_connect.php')) {
    die("Error: db_connect.php file is missing.");
}
require_once 'db_connect.php';

// 2. Security Check
if (!isset($_SESSION['student_id'])) { 
    header("Location: login.php"); 
    exit; 
}

$student_id = $_SESSION['student_id'];
$course_id = $_SESSION['course_id'] ?? 0;

// 3. Fetch Live Exams logic (Only shows exams the student hasn't taken yet)
try {
    $stmt = $pdo->prepare("
        SELECT * FROM exams 
        WHERE course_id = ? 
        AND status = 'active' 
        AND id NOT IN (SELECT exam_id FROM exam_result WHERE student_id = ?)
        ORDER BY id DESC
    ");
    $stmt->execute([$course_id, $student_id]);
    $available_exams = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Exam Fetch Error: " . $e->getMessage());
}

// 4. Include Header & Sidebar
if(file_exists('includes/user_header.php')) { include 'includes/user_header.php'; } 
elseif(file_exists('header.php')) { include 'header.php'; }

if(file_exists('includes/sidebar.php')) { include 'includes/sidebar.php'; } 
elseif(file_exists('sidebar.php')) { include 'sidebar.php'; }
?>

<style>
    :root {
        --primary: #064e3b;
        --primary-dark: #022c22;
        --accent: #d4af37; /* Golden */
    }

    /* Live Blinking Animation */
    @keyframes blink { 0% { opacity: 1; } 50% { opacity: 0; } 100% { opacity: 1; } }
    
    /* Exam Card Styles */
    .exam-card {
        border-top: 4px solid var(--primary) !important;
        transition: all 0.3s ease !important;
    }
    .exam-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 15px 35px rgba(0,0,0,0.08) !important;
        border-color: var(--accent) !important;
    }
    
    /* Start Button Styles */
    .btn-start-exam {
        background: var(--primary);
        color: #fff;
        border: none;
        padding: 12px;
        transition: 0.3s;
    }
    .btn-start-exam:hover {
        background: var(--accent);
        color: var(--primary-dark);
        transform: translateY(-2px);
        box-shadow: 0 8px 20px rgba(212, 175, 55, 0.3) !important;
    }
</style>

<div class="main-content p-4" style="background: #f8fafc; min-height: 80vh;">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="fw-bold text-dark"><i class="fas fa-edit me-2" style="color: var(--accent);"></i> Live Exams</h4>
    </div>

    <div class="row g-4">
        <?php if(!empty($available_exams)): foreach($available_exams as $exam): ?>
        
        <div class="col-md-6 col-lg-4">
            <div class="card border-0 shadow-sm rounded-4 h-100 exam-card">
                <div class="card-body p-4">
                    
                    <span class="badge bg-danger bg-opacity-10 text-danger rounded-pill mb-3 px-3 py-2 fw-bold border border-danger border-opacity-25">
                        <i class="fas fa-circle text-danger me-1" style="font-size: 8px; vertical-align: middle; animation: blink 1.5s infinite;"></i> Live Now
                    </span>
                    
                    <h5 class="fw-bold text-dark mb-3"><?= htmlspecialchars($exam['exam_title'] ?? 'Unknown Exam'); ?></h5>
                    
                    <ul class="list-unstyled text-muted small mb-4 fw-medium">
                        <li class="mb-2"><i class="far fa-clock me-2" style="color: var(--accent);"></i> Duration: <?= htmlspecialchars($exam['duration_minutes'] ?? 0); ?> Minutes</li>
                        <li class="mb-2"><i class="fas fa-list-ol me-2" style="color: var(--accent);"></i> Total Questions: <?= htmlspecialchars($exam['total_questions'] ?? 0); ?></li>
                    </ul>
                    
                    <a href="take_exam.php?id=<?= $exam['id']; ?>" class="btn w-100 rounded-pill fw-bold shadow-sm btn-start-exam">
                        Start Exam <i class="fas fa-arrow-right ms-2"></i>
                    </a>
                    
                </div>
            </div>
        </div>

        <?php endforeach; else: ?>
        
        <div class="col-12 text-center py-5">
            <div class="mb-4">
                <i class="fas fa-clipboard-check opacity-25" style="font-size: 80px; color: var(--primary);"></i>
            </div>
            <h5 class="text-muted fw-bold">There are no pending live exams for your course at the moment.</h5>
            <p class="text-muted small">You have completed all available exams or no new exams have been added yet.</p>
        </div>
        
        <?php endif; ?>
    </div>
</div>

<script>
// 1. Disable Right Click
document.addEventListener('contextmenu', event => event.preventDefault());

// 2. Disable Copy, Paste, and Cut
document.addEventListener('copy', e => e.preventDefault());
document.addEventListener('paste', e => e.preventDefault());
document.addEventListener('cut', e => e.preventDefault());

// 3. Disable Keyboard Shortcuts (F12, Ctrl+Shift+I, Ctrl+U)
document.onkeydown = function(e) {
    if (e.keyCode == 123) return false; // F12
    if (e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) return false;
    if (e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) return false;
    if (e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) return false;
    if (e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)) return false; // View Source
};
</script>

<?php 
// 5. Include Footer
if(file_exists('includes/user_footer.php')) { include 'includes/user_footer.php'; } 
elseif(file_exists('footer.php')) { include 'footer.php'; }
?>