<?php
session_start();
header('Content-Type: application/json');

require_once '../../config/database.php';
require_once '../../includes/access_control.php';

// Check if user is logged in and is a teacher
require_teacher();

// Get question ID from URL
$question_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($question_id <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid question ID']);
    exit();
}

try {
    // Fetch question details
    $stmt = $pdo->prepare("
        SELECT eq.*, e.name as exam_name, s.name as subject_name
        FROM exam_questions eq
        JOIN exams e ON eq.exam_id = e.id
        JOIN subjects s ON eq.subject_id = s.id
        WHERE eq.id = ?
    ");
    $stmt->execute([$question_id]);
    $question = $stmt->fetch();
    
    if (!$question) {
        echo json_encode(['success' => false, 'message' => 'Question not found']);
        exit();
    }
    
    echo json_encode(['success' => true, 'question' => $question]);
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Error fetching question: ' . $e->getMessage()]);
}