<?php

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

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

$question_id = $_POST['question_id'] ?? '';
$status = $_POST['status'] ?? '';

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

$allowed_statuses = ['Draft', 'Done', 'Rejected'];
if (!in_array($status, $allowed_statuses, true)) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Invalid status value.'
    ]);
    exit;
}

$sql = "UPDATE questions SET status = :status WHERE id = :question_id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
    'status' => $status,
    'question_id' => $question_id
]);

echo json_encode([
    'status' => 'success',
    'message' => 'Question status updated.'
]);