<?php
require_once '../includes/init.php';

$user = new User($pdo);
if (!$user->isLoggedIn() || !$user->isTeacher()) {
    header("Location: ../index.php");
    exit;
}

if (!isset($_GET['exam_id'])) {
    die("Exam ID required");
}

$exam_id = $_GET['exam_id'];

// Fetch Exam
$stmt = $pdo->prepare("SELECT * FROM exams WHERE exam_id = ?");
$stmt->execute([$exam_id]);
$exam = $stmt->fetch();
if (!$exam) die("Exam not found");

// Load Questions logic
$order_clause = $exam['randomize_questions'] ? "ORDER BY RAND()" : "ORDER BY question_id ASC";
$conditions = ["q.subject_id = ?"];
$params = [$exam['subject_id']];

if (!empty($exam['class_id'])) {
    $conditions[] = "q.class_id = ?";
    $params[] = $exam['class_id'];
}
if (!empty($exam['session'])) {
    $conditions[] = "q.session = ?";
    $params[] = $exam['session'];
}
if (!empty($exam['term'])) {
    $conditions[] = "q.term = ?";
    $params[] = $exam['term'];
}

$limit_clause = "";
if (!empty($exam['question_quota']) && $exam['question_quota'] > 0) {
    $limit_clause = " LIMIT " . (int)$exam['question_quota'];
}

$sql = "SELECT q.*, p.content as passage_content, p.title as passage_title, m.file_path as media_path 
        FROM questions q 
        LEFT JOIN passages p ON q.passage_id = p.passage_id 
        LEFT JOIN media_library m ON q.media_id = m.media_id 
        WHERE " . implode(" AND ", $conditions) . " $order_clause $limit_clause";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$questions = $stmt->fetchAll();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Preview: <?php echo htmlspecialchars($exam['title']); ?></title>
    <link rel="stylesheet" href="../assets/css/student.css">
    <style>
        body { background: #f4f4f4; padding: 20px; font-family: sans-serif; }
        .preview-header { background: #fff; padding: 20px; margin-bottom: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        .question-box { background: #fff; padding: 20px; margin-bottom: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        .correct-answer { color: green; font-weight: bold; }
    </style>
</head>
<body>
    <div class="preview-header">
        <h1>Preview: <?php echo htmlspecialchars($exam['title']); ?></h1>
        <p><strong>Duration:</strong> <?php echo $exam['duration']; ?> mins</p>
        <p><strong>Total Questions:</strong> <?php echo count($questions); ?></p>
        <p style="color:red;">This is a preview. No answers are saved.</p>
    </div>

    <?php if (empty($questions)): ?>
        <p>No questions found for this exam criteria.</p>
    <?php else: ?>
        <?php foreach ($questions as $index => $q): ?>
            <div class="question-box">
                <p><strong>Q<?php echo $index + 1; ?>.</strong></p>
                
                <?php if (!empty($q['passage_content'])): ?>
                    <div class="passage" style="background:#f9f9f9; padding:15px; border-left: 4px solid #007bff; margin-bottom:15px;">
                        <strong><?php echo htmlspecialchars($q['passage_title']); ?></strong>
                        <div><?php echo $q['passage_content']; ?></div>
                    </div>
                <?php endif; ?>

                <?php if (!empty($q['media_path'])): ?>
                    <div class="media" style="margin-bottom:15px;">
                        <img src="../<?php echo $q['media_path']; ?>" style="max-width:100%; max-height:300px;">
                    </div>
                <?php endif; ?>

                <div class="question-text"><?php echo $q['question_text']; ?></div>
                
                <?php if ($q['image_path']): ?>
                    <img src="../<?php echo $q['image_path']; ?>" style="max-width: 200px; margin: 10px 0;">
                <?php endif; ?>

                <ul style="list-style: none; padding: 0;">
                    <?php foreach (['A','B','C','D','E'] as $opt): ?>
                        <?php if (!empty($q['option_' . strtolower($opt)])): ?>
                            <li style="<?php echo ($q['correct_answer'] == $opt) ? 'color:green; font-weight:bold; border: 1px solid green; padding: 5px; border-radius: 4px;' : 'padding: 5px;'; ?>">
                                <span style="font-weight: bold; margin-right: 5px;"><?php echo $opt; ?>:</span>
                                <div style="display: inline-block; vertical-align: top;">
                                    <?php echo $q['option_' . strtolower($opt)]; ?>
                                </div>
                                <?php echo ($q['correct_answer'] == $opt) ? ' (Correct)' : ''; ?>
                            </li>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
    
    <div style="text-align: center; margin-top: 30px; padding: 20px; color: #666; font-size: 0.9em; border-top: 1px solid #eee;">
        &copy; 2026 Designed by FGV
    </div>
</body>
</html>
