<?php

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

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

$workspace_id = $_POST['workspace_id'] ?? '';
$specialty = $_POST['specialty'] ?? '';
$core_condition = $_POST['core_condition'] ?? '';
$presentation = $_POST['presentation'] ?? '';
$learning_outcome = $_POST['learning_outcome'] ?? '';
$num_questions = $_POST['num_questions'] ?? '';

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

$title = 'Test SBA Question';
$stem = 'A 45-year-old man presents with wheeze and shortness of breath. He has a history of asthma.';
$lead_in = 'What is the most likely diagnosis?';
$option_a = 'Pneumonia';
$option_b = 'Asthma exacerbation';
$option_c = 'Pulmonary embolism';
$option_d = 'Heart failure';
$option_e = 'Pneumothorax';
$correct_answer_letter = 'B';
$correct_answer = 'Asthma exacerbation';
$justification = 'The history of wheeze, breathlessness and known asthma makes asthma exacerbation the best answer.';

$sql = "INSERT INTO questions (
    workspace_id,
    source_type,
    title,
    stem,
    lead_in,
    option_a,
    option_b,
    option_c,
    option_d,
    option_e,
    correct_answer,
    correct_answer_letter,
    justification,
    specialty,
    condition_name,
    presentation,
    skill,
    year_level,
    tags,
    status
) VALUES (
    :workspace_id,
    :source_type,
    :title,
    :stem,
    :lead_in,
    :option_a,
    :option_b,
    :option_c,
    :option_d,
    :option_e,
    :correct_answer,
    :correct_answer_letter,
    :justification,
    :specialty,
    :condition_name,
    :presentation,
    :skill,
    :year_level,
    :tags,
    :status
)";

$stmt = $pdo->prepare($sql);

$stmt->execute([
    'workspace_id' => $workspace_id,
    'source_type' => 'generated',
    'title' => $title,
    'stem' => $stem,
    'lead_in' => $lead_in,
    'option_a' => $option_a,
    'option_b' => $option_b,
    'option_c' => $option_c,
    'option_d' => $option_d,
    'option_e' => $option_e,
    'correct_answer' => $correct_answer,
    'correct_answer_letter' => $correct_answer_letter,
    'justification' => $justification,
    'specialty' => $specialty,
    'condition_name' => $core_condition,
    'presentation' => $presentation,
    'skill' => 'Diagnosis',
    'year_level' => 4,
    'tags' => 'test,sba',
    'status' => 'Draft'
]);

$question_id = $pdo->lastInsertId();

$response = [
    'status' => 'success',
    'message' => 'Question generated and saved.',
    'question' => [
        'id' => $question_id,
        'title' => $title,
        'stem' => $stem,
        'lead_in' => $lead_in,
        'option_a' => $option_a,
        'option_b' => $option_b,
        'option_c' => $option_c,
        'option_d' => $option_d,
        'option_e' => $option_e,
        'correct_answer_letter' => $correct_answer_letter,
        'correct_answer' => $correct_answer,
        'justification' => $justification,
        'specialty' => $specialty,
        'condition_name' => $core_condition,
        'presentation' => $presentation,
        'learning_outcome' => $learning_outcome,
        'workspace_id' => $workspace_id,
        'num_questions' => $num_questions
    ]
];

echo json_encode($response);