<?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 {
    $question_id = (int)$_POST['question_id'];
    $question_text = trim($_POST['question_text']);
    $question_type = $_POST['question_type'];
    $marks = (int)$_POST['marks'];
    $question_order = (int)$_POST['question_order'];
    
    if (empty($question_text) || empty($question_type) || $marks <= 0) {
        echo json_encode(['success' => false, 'message' => 'সকল প্রয়োজনীয় তথ্য পূরণ করুন']);
        exit();
    }
    
    // Get the exam_id for this question to verify ownership
    $stmt = $pdo->prepare("SELECT exam_id FROM exam_questions WHERE id = ?");
    $stmt->execute([$question_id]);
    $question = $stmt->fetch();
    
    if (!$question) {
        echo json_encode(['success' => false, 'message' => 'Question not found']);
        exit();
    }
    
    $exam_id = $question['exam_id'];
    
    // Get subject_id from the exam
    $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) {
        echo json_encode(['success' => false, 'message' => 'Exam subject not found']);
        exit();
    }
    
    $subject_id = $exam_subject['subject_id'];
    
    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();
        }
        
        // Update MCQ question
        $stmt = $pdo->prepare("
            UPDATE exam_questions 
            SET subject_id = ?, question_text = ?, question_type = ?, option_a = ?, option_b = ?, option_c = ?, option_d = ?, correct_answer = ?, marks = ?, question_order = ?
            WHERE id = ?
        ");
        $stmt->execute([
            $subject_id, 
            $question_text, 
            $question_type, 
            $option_a, 
            $option_b, 
            $option_c, 
            $option_d, 
            $correct_answer, 
            $marks,
            $question_order,
            $question_id
        ]);
    } else {
        // Update short or long question
        $stmt = $pdo->prepare("
            UPDATE exam_questions 
            SET subject_id = ?, question_text = ?, question_type = ?, marks = ?, question_order = ?
            WHERE id = ?
        ");
        $stmt->execute([
            $subject_id, 
            $question_text, 
            $question_type, 
            $marks,
            $question_order,
            $question_id
        ]);
    }
    
    echo json_encode(['success' => true, 'message' => 'প্রশ্ন সফলভাবে আপডেট হয়েছে!']);
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'প্রশ্ন আপডেট করতে সমস্যা হয়েছে: ' . $e->getMessage()]);
}