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

// Check if user is logged in and is admin/teacher
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['user_role'], ['admin', 'teacher'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized access']);
    exit();
}

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

try {
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $online_exam_id = $_POST['online_exam_id'] ?? null;
        $exam_id = $_POST['exam_id'] ?? null;
        $subject_id = $_POST['subject_id'] ?? null;
        $question_text = $_POST['question_text'] ?? '';
        $question_type = $_POST['question_type'] ?? 'mcq';
        $marks = $_POST['marks'] ?? 1;
        
        // Determine which table to use
        $is_online = !empty($online_exam_id);
        $exam_identifier = $is_online ? $online_exam_id : $exam_id;
        
        if (!$exam_identifier || !$subject_id || empty($question_text)) {
            echo json_encode(['success' => false, 'message' => 'Required fields are missing']);
            exit();
        }
        
        // Get subject_id from exam if not provided
        if (!$subject_id) {
            if ($is_online) {
                $stmt = $pdo->prepare("SELECT subject_id FROM online_exams WHERE id = ?");
                $stmt->execute([$online_exam_id]);
            } else {
                $stmt = $pdo->prepare("SELECT subject_id FROM exam_subjects WHERE exam_id = ?");
                $stmt->execute([$exam_id]);
            }
            $result = $stmt->fetch();
            if ($result) {
                $subject_id = $result['subject_id'];
            }
        }
        
        // Insert question based on type and exam type
        if ($question_type === 'mcq') {
            $option_a = $_POST['option_a'] ?? '';
            $option_b = $_POST['option_b'] ?? '';
            $option_c = $_POST['option_c'] ?? '';
            $option_d = $_POST['option_d'] ?? '';
            $correct_answer = $_POST['correct_answer'] ?? '';
            
            if ($is_online) {
                $stmt = $pdo->prepare("INSERT INTO online_exam_questions (online_exam_id, subject_id, question_text, question_type, option_a, option_b, option_c, option_d, correct_answer, marks) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                $stmt->execute([$online_exam_id, $subject_id, $question_text, $question_type, $option_a, $option_b, $option_c, $option_d, $correct_answer, $marks]);
            } else {
                $stmt = $pdo->prepare("INSERT INTO exam_questions (exam_id, subject_id, question_text, question_type, option_a, option_b, option_c, option_d, correct_answer, marks) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                $stmt->execute([$exam_id, $subject_id, $question_text, $question_type, $option_a, $option_b, $option_c, $option_d, $correct_answer, $marks]);
            }
        } else {
            // For short/long questions
            if ($is_online) {
                $stmt = $pdo->prepare("INSERT INTO online_exam_questions (online_exam_id, subject_id, question_text, question_type, marks) VALUES (?, ?, ?, ?, ?)");
                $stmt->execute([$online_exam_id, $subject_id, $question_text, $question_type, $marks]);
            } else {
                $stmt = $pdo->prepare("INSERT INTO exam_questions (exam_id, subject_id, question_text, question_type, marks) VALUES (?, ?, ?, ?, ?)");
                $stmt->execute([$exam_id, $subject_id, $question_text, $question_type, $marks]);
            }
        }
        
        echo json_encode(['success' => true, 'message' => 'Question added successfully']);
    } else {
        echo json_encode(['success' => false, 'message' => 'Invalid request method']);
    }
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
?>