<?php

header('Content-Type: application/json');

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../ai/form_generator_question_validation.php';

$question_id = $_GET['question_id'] ?? '';

if (empty($question_id)) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Missing question ID.'
    ]);
    exit;
}

$sql = "SELECT * FROM questions WHERE id = :question_id LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute([
    'question_id' => $question_id
]);

$question = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$question) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Question not found.'
    ]);
    exit;
}

$question = formGeneratorApplyStyleRepairToQuestionRow($question);

echo json_encode([
    'status' => 'success',
    'question' => $question
]);