<?php
// teacher/add_questions_manual.php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require_once '../config/database.php';

// 1. SECURITY: Teachers only
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'teacher') {
    header("Location: ../auth/login.php");
    exit();
}

// 2. GET EXAM DETAILS
if (!isset($_GET['exam_id'])) {
    header("Location: dashboard.php");
    exit();
}

$exam_id = (int)$_GET['exam_id'];
$teacher_id = (int)$_SESSION['user_id'];

// Verify the exam belongs to this teacher
$stmt = $pdo->prepare("SELECT exam_title FROM exams WHERE id = ? AND teacher_id = ?");
$stmt->execute([$exam_id, $teacher_id]);
$exam = $stmt->fetch();

if (!$exam) {
    die("Error: Exam not found or access denied.");
}

// 3. HANDLE FORM SUBMISSION
$message = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $question_text = trim($_POST['question_text']);
    $option_a = trim($_POST['option_a']);
    $option_b = trim($_POST['option_b']);
    $option_c = trim($_POST['option_c']);
    $option_d = trim($_POST['option_d']);
    $correct_option = $_POST['correct_option'];

    try {
        $ins = $pdo->prepare("INSERT INTO questions (exam_id, question_text, option_a, option_b, option_c, option_d, correct_option) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $ins->execute([$exam_id, $question_text, $option_a, $option_b, $option_c, $option_d, $correct_option]);
        $message = "<div class='alert success'><i class='fa fa-check-circle'></i> Question added successfully!</div>";
    } catch (PDOException $e) {
        $message = "<div class='alert error'>Error: " . $e->getMessage() . "</div>";
    }
}

// 4. COUNT CURRENT QUESTIONS
$count_stmt = $pdo->prepare("SELECT COUNT(*) FROM questions WHERE exam_id = ?");
$count_stmt->execute([$exam_id]);
$q_count = $count_stmt->fetchColumn();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Questions - <?= htmlspecialchars($exam['exam_title']) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #1e293b; --accent: #2563eb; --bg: #f8fafc; --text-muted: #64748b; --success: #22c55e; }
        * { box-sizing: border-box; }
        body { font-family: 'Inter', sans-serif; background: var(--bg); color: var(--primary); margin: 0; padding: 20px; }
        
        .container { max-width: 700px; margin: 0 auto; }
        .nav-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        .btn-back { text-decoration: none; color: var(--text-muted); font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 5px; }
        
        .card { background: white; padding: 30px; border-radius: 16px; box-shadow: 0 4px 10px rgba(0,0,0,0.05); }
        .header-area { border-bottom: 1px solid #f1f5f9; margin-bottom: 25px; padding-bottom: 15px; }
        .badge { background: #eff6ff; color: var(--accent); padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: 700; }

        .form-group { margin-bottom: 20px; }
        label { display: block; font-weight: 600; margin-bottom: 8px; font-size: 14px; }
        
        textarea, input[type="text"] { 
            width: 100%; padding: 12px; border: 1px solid #e2e8f0; border-radius: 8px; 
            font-size: 15px; font-family: inherit; outline: none; transition: 0.2s;
        }
        textarea:focus, input:focus { border-color: var(--accent); }
        
        .options-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
        .option-item { position: relative; }
        .option-label { position: absolute; left: 12px; top: 12px; font-weight: 800; color: var(--text-muted); }
        .option-input { padding-left: 35px !important; }

        .correct-selector { margin-top: 20px; background: #f8fafc; padding: 15px; border-radius: 10px; border: 1px dashed #cbd5e1; }
        .radio-group { display: flex; gap: 20px; margin-top: 10px; }
        .radio-group label { display: flex; align-items: center; gap: 5px; cursor: pointer; }

        .btn-save { 
            width: 100%; background: var(--primary); color: white; padding: 16px; 
            border: none; border-radius: 10px; font-weight: 700; font-size: 16px; 
            cursor: pointer; margin-top: 20px; transition: 0.3s; 
        }
        .btn-save:hover { background: #000; }
        .btn-finish { 
            display: block; text-align: center; margin-top: 15px; color: var(--text-muted); 
            text-decoration: none; font-size: 14px; font-weight: 600; 
        }

        .alert { padding: 15px; border-radius: 8px; margin-bottom: 20px; font-size: 14px; }
        .alert.success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
        
        @media (max-width: 600px) {
            .options-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>

<div class="container">
    <div class="nav-top">
        <a href="dashboard.php" class="btn-back"><i class="fa fa-arrow-left"></i> Exit to Dashboard</a>
        <span class="badge">Questions Added: <?= $q_count ?></span>
    </div>

    <div class="card">
        <div class="header-area">
            <h2 style="margin:0; font-size: 20px;"><?= htmlspecialchars($exam['exam_title']) ?></h2>
            <p style="margin: 5px 0 0 0; color: var(--text-muted); font-size: 13px;">Enter question details and select the correct answer.</p>
        </div>

        <?= $message ?>

        <form method="POST">
            <div class="form-group">
                <label>Question Text</label>
                <textarea name="question_text" rows="3" placeholder="Type your question here..." required></textarea>
            </div>

            <div class="options-grid">
                <div class="form-group option-item">
                    <span class="option-label">A</span>
                    <input type="text" name="option_a" class="option-input" placeholder="Option A" required>
                </div>
                <div class="form-group option-item">
                    <span class="option-label">B</span>
                    <input type="text" name="option_b" class="option-input" placeholder="Option B" required>
                </div>
                <div class="form-group option-item">
                    <span class="option-label">C</span>
                    <input type="text" name="option_c" class="option-input" placeholder="Option C" required>
                </div>
                <div class="form-group option-item">
                    <span class="option-label">D</span>
                    <input type="text" name="option_d" class="option-input" placeholder="Option D" required>
                </div>
            </div>

            <div class="correct-selector">
                <label style="margin-bottom:0;">Select Correct Option</label>
                <div class="radio-group">
                    <label><input type="radio" name="correct_option" value="A" required> A</label>
                    <label><input type="radio" name="correct_option" value="B"> B</label>
                    <label><input type="radio" name="correct_option" value="C"> C</label>
                    <label><input type="radio" name="correct_option" value="D"> D</label>
                </div>
            </div>

            <button type="submit" class="btn-save">
                <i class="fa fa-plus"></i> Save and Add Another
            </button>
            
            <a href="manage_exams.php" class="btn-finish">I'm finished, view exam list</a>
        </form>
    </div>
</div>

</body>
</html>