<?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();

// Only allow POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Invalid request method']);
    exit();
}

try {
    // Handle both online_exam_id and exam_id
    $exam_id = 0;
    if (isset($_POST['online_exam_id']) && (int)$_POST['online_exam_id'] > 0) {
        $exam_id = (int)$_POST['online_exam_id'];
    } else if (isset($_POST['exam_id']) && (int)$_POST['exam_id'] > 0) {
        $exam_id = (int)$_POST['exam_id'];
    }
    
    // Get subject_id from the exam if not provided
    $subject_id = 0;
    if (isset($_POST['subject_id']) && (int)$_POST['subject_id'] > 0) {
        $subject_id = (int)$_POST['subject_id'];
    } else if ($exam_id > 0) {
        // Try to get subject_id from exam_subjects table
        $stmt = $pdo->prepare("SELECT subject_id FROM exam_subjects WHERE exam_id = ? LIMIT 1");
        $stmt->execute([$exam_id]);
        $exam_subject = $stmt->fetch();
        if ($exam_subject) {
            $subject_id = $exam_subject['subject_id'];
        }
    }
    
    $question_text = trim($_POST['question_text']);
    $question_type = $_POST['question_type'];
    $marks = (int)$_POST['marks'];
    $chapter_topic = isset($_POST['chapter_topic']) ? trim($_POST['chapter_topic']) : null;
    $difficulty_level = isset($_POST['difficulty_level']) ? $_POST['difficulty_level'] : 'medium';
    $is_starred = isset($_POST['is_starred']) ? 1 : 0;
    
    if (empty($question_text) || empty($question_type) || $marks <= 0) {
        echo json_encode(['success' => false, 'message' => 'সকল প্রয়োজনীয় তথ্য পূরণ করুন']);
        exit();
    }
    
    if ($exam_id <= 0) {
        echo json_encode(['success' => false, 'message' => 'পরীক্ষা নির্বাচন করুন']);
        exit();
    }
    
    if ($subject_id <= 0) {
        echo json_encode(['success' => false, 'message' => 'বিষয় নির্বাচন করুন']);
        exit();
    }
    
    if ($question_type === 'mcq') {
        $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_answer = $_POST['correct_answer'];
        
        if (empty($option_a) || empty($option_b) || empty($option_c) || empty($option_d) || empty($correct_answer)) {
            echo json_encode(['success' => false, 'message' => 'MCQ প্রশ্নের জন্য সকল অপশন এবং সঠিক উত্তর প্রদান করুন']);
            exit();
        }
        
        // Insert MCQ question
        $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, chapter_topic, difficulty_level, is_starred)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        ");
        $stmt->execute([
            $exam_id, 
            $subject_id, 
            $question_text, 
            $question_type, 
            $option_a, 
            $option_b, 
            $option_c, 
            $option_d, 
            $correct_answer, 
            $marks,
            $chapter_topic,
            $difficulty_level,
            $is_starred
        ]);
    } else {
        // Insert short or long question
        $stmt = $pdo->prepare("
            INSERT INTO exam_questions (exam_id, subject_id, question_text, question_type, marks, chapter_topic, difficulty_level, is_starred)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        ");
        $stmt->execute([
            $exam_id, 
            $subject_id, 
            $question_text, 
            $question_type, 
            $marks,
            $chapter_topic,
            $difficulty_level,
            $is_starred
        ]);
    }
    
    echo json_encode(['success' => true, 'message' => 'প্রশ্ন সফলভাবে যোগ হয়েছে!']);
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'প্রশ্ন যোগ করতে সমস্যা হয়েছে: ' . $e->getMessage()]);
}