<?php
session_start();
include '../admin/includes/database.php';
include '../admin/includes/functions.php';
$conn= new Functions();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        $question_id = $_POST['question_id'];
        $question_text = trim($_POST['question_text']);
        $question_type = $_POST['question_type'];

        if (empty($question_text)) {
            throw new Exception("Question text cannot be empty.");
        }

        // Handle image upload
        $conn->query("SELECT question_image FROM cbt_questions WHERE id = :question_id");
        $conn->bind(':question_id', $question_id);
        $existing_image = $conn->fetchColumn();

        $image_path = $existing_image; // Default to existing image
        if (!empty($_FILES['question_image']['name'])) {
            $image_name = time() . '_' . $_FILES['question_image']['name'];
            $target_dir = "../admin/upload/questions/";
            $target_file = $target_dir . basename($image_name);

            if (move_uploaded_file($_FILES['question_image']['tmp_name'], $target_file)) {
                $image_path = $target_file;
            }
        }

        // Update the question text and image
        $sql = "UPDATE cbt_questions SET question_text = :question_text, question_image = :question_image WHERE id = :question_id";
        $conn->query($sql);
        $conn->bind(':question_text', $question_text);
        $conn->bind(':question_image', $image_path);
        $conn->bind(':question_id', $question_id);
        $conn->execute();

        // Handle MCQ options update
        if ($question_type === 'mcq') {
            if (empty($_POST['options'])) {
                throw new Exception("MCQ must have options.");
            }
            if (!isset($_POST['correct_option'])) {
                throw new Exception("Please select a correct answer for MCQ.");
            }

            // Delete existing options before inserting new ones
            $conn->query("DELETE FROM cbt_options WHERE question_id = :question_id");
            $conn->bind(':question_id', $question_id);
            $conn->execute();

            // Insert updated options
            foreach ($_POST['options'] as $index => $option_text) {
                $is_correct = ($_POST['correct_option'] == $index) ? 1 : 0;

                $sql = "INSERT INTO cbt_options (question_id, option_text, is_correct) VALUES (:question_id, :option_text, :is_correct)";
                $conn->query($sql);
                $conn->bind(':question_id', $question_id);
                $conn->bind(':option_text', $option_text);
                $conn->bind(':is_correct', $is_correct);
                $conn->execute();
            }
        }

        // Update correct answer for Fill-in-the-Blank and True/False questions
        if ($question_type === 'fill_blank' || $question_type === 'true_false') {
            $correct_answer = ($question_type === 'fill_blank') ? $_POST['fill_blank_text'] : $_POST['true_false_answer'];

            // Delete old correct answer
            $conn->query("DELETE FROM cbt_options WHERE question_id = :question_id");
            $conn->bind(':question_id', $question_id);
            $conn->execute();

            // Insert updated correct answer
            $sql = "INSERT INTO cbt_options (question_id, option_text, is_correct) VALUES (:question_id, :option_text, 1)";
            $conn->query($sql);
            $conn->bind(':question_id', $question_id);
            $conn->bind(':option_text', $correct_answer);
            $conn->execute();
        }

        echo json_encode(['status' => 'success', 'message' => 'Question updated successfully']);
    } catch (Exception $e) {
        echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request']);
}





