<?php

/**
 * Run clinical safety reviewer on four PE/ACS scenarios.
 * Usage: php tools/test_clinical_safety_reviewer_scenarios.php
 */

require dirname(__DIR__) . '/app/config/config.php';
require dirname(__DIR__) . '/prompts/sba_clinical_safety_reviewer_prompt.php';
require dirname(__DIR__) . '/app/ai/form_generator_retry.php';

$base = [
    'specialty' => 'Acute Medicine',
    'condition_name' => 'Pulmonary embolism',
    'presentation' => 'Suspected PE',
    'year_level' => 'MLA Final Year',
    'summative_formative' => 'Summative',
    'tutor_comments' => '',
];

$scenarios = [
    '1_pe_ctpa_negative_anticoag_keyed' => [
        'title' => 'PE anticoagulation after negative CTPA',
        'skill' => 'Management',
        'stem' => 'A 45 year old woman presents with pleuritic chest pain and mild breathlessness. Heart rate 98/min, blood pressure 118/72 mmHg, oxygen saturation 97% on air. D-dimer was elevated. CTPA shows no pulmonary embolism. No other indication for anticoagulation is documented.',
        'lead_in' => 'What is the most appropriate next management step?',
        'option_a' => 'Start therapeutic-dose rivaroxaban for pulmonary embolism',
        'option_b' => 'Repeat CTPA in 24 hours',
        'option_c' => 'Arrange V/Q scan before any treatment decision',
        'option_d' => 'Discharge with safety-net advice and treat alternative cause',
        'option_e' => 'Start low molecular weight heparin pending further imaging',
        'correct_answer_letter' => 'A',
        'correct_answer' => 'Start therapeutic-dose rivaroxaban for pulmonary embolism',
        'justification' => 'Anticoagulation is required for confirmed pulmonary embolism.',
        'style_warnings' => [],
        'expect_faculty' => true,
    ],
    '2_pe_rivaroxaban_pt_inr_monitoring' => [
        'title' => 'Monitoring on rivaroxaban for PE',
        'skill' => 'Management',
        'stem' => 'A 62 year old man was diagnosed with pulmonary embolism 3 months ago and is stable on rivaroxaban 20 mg once daily. Renal function is normal. He attends for routine follow-up with no bleeding symptoms.',
        'lead_in' => 'What is the most appropriate monitoring for this patient?',
        'option_a' => 'Check PT and INR monthly',
        'option_b' => 'Check renal function, FBC, liver function and assess bleeding risk and adherence',
        'option_c' => 'Check anti-Xa levels every 2 weeks',
        'option_d' => 'No monitoring is ever required on rivaroxaban',
        'option_e' => 'Switch to warfarin for easier INR monitoring',
        'correct_answer_letter' => 'A',
        'correct_answer' => 'Check PT and INR monthly',
        'justification' => 'Patients on rivaroxaban require regular PT and INR monitoring like warfarin.',
        'style_warnings' => [],
        'expect_faculty' => true,
    ],
    '3_pe_diagnosis_sound' => [
        'title' => 'PE diagnosis with positive CTPA',
        'skill' => 'Diagnosis',
        'stem' => 'A 58 year old woman presents with sudden pleuritic chest pain and breathlessness after a long-haul flight. Heart rate 110/min, blood pressure 102/68 mmHg, oxygen saturation 91% on air. Wells score is high. CTPA shows bilateral segmental pulmonary emboli.',
        'lead_in' => 'What is the most likely diagnosis?',
        'option_a' => 'Pulmonary embolism',
        'option_b' => 'Acute coronary syndrome',
        'option_c' => 'Pneumothorax',
        'option_d' => 'Community-acquired pneumonia',
        'option_e' => 'Pericarditis',
        'correct_answer_letter' => 'A',
        'correct_answer' => 'Pulmonary embolism',
        'justification' => 'High pre-test probability with segmental filling defects on CTPA confirms pulmonary embolism.',
        'style_warnings' => [],
        'expect_faculty' => false,
    ],
    '4_acs_interpretation_sound' => [
        'title' => 'ACS troponin interpretation',
        'skill' => 'Interpretation',
        'stem' => 'A 67 year old man with cardiovascular risk factors has central chest pain lasting 40 minutes. ECG shows 2 mm ST elevation in leads V2–V4. High-sensitivity troponin is elevated and rising at 2 hours.',
        'lead_in' => 'What is the most likely diagnosis?',
        'option_a' => 'ST-elevation myocardial infarction',
        'option_b' => 'Non-ST elevation myocardial infarction',
        'option_c' => 'Unstable angina with normal troponin',
        'option_d' => 'Pulmonary embolism',
        'option_e' => 'Aortic dissection',
        'correct_answer_letter' => 'A',
        'correct_answer' => 'ST-elevation myocardial infarction',
        'justification' => 'ST elevation with rising troponin indicates STEMI.',
        'style_warnings' => [],
        'expect_faculty' => false,
    ],
];

$passed = 0;
$failed = 0;

foreach ($scenarios as $key => $scenario) {
    $warnings = $scenario['style_warnings'];
    $expectFaculty = $scenario['expect_faculty'] ?? false;
    unset($scenario['style_warnings'], $scenario['expect_faculty']);

    $q = array_merge($base, $scenario);
    $prompt = buildSbaClinicalSafetyReviewerPrompt($q, $warnings);

    echo "\n========== {$key} ==========\n";

    $fetch = formGeneratorOpenAiChatJsonQuestion(
        $prompt,
        '',
        0,
        0.2,
        formGeneratorOpenAiSbaSafetyReviewModel()
    );
    if (!$fetch['ok']) {
        echo 'FAIL fetch: ' . $fetch['error'] . "\n";
        ++$failed;
        continue;
    }

    $norm = normalizeSbaClinicalSafetyReviewerResponse($fetch['data']);
    if (!$norm['ok']) {
        echo 'FAIL normalize: ' . $norm['error'] . "\n";
        ++$failed;
        continue;
    }

    $r = $norm['data'];
    echo 'Safety: ' . $r['clinical_safety_rating'] . ' | Accuracy: ' . $r['clinical_accuracy_rating']
        . ' | OBA: ' . $r['one_best_answer_fairness'] . ' | Faculty: ' . ($r['faculty_review_required'] ? 'yes' : 'no') . "\n";
    echo 'Summary: ' . $r['short_summary'] . "\n";
    if ($r['high_priority_concerns'] !== []) {
        echo 'High priority: ' . implode('; ', $r['high_priority_concerns']) . "\n";
    }

    $facultyOk = ($expectFaculty && $r['faculty_review_required']) || (!$expectFaculty && !$r['faculty_review_required']);
    if ($facultyOk) {
        echo "PASS\n";
        ++$passed;
    } else {
        echo "FAIL faculty expectation (expected faculty=" . ($expectFaculty ? 'true' : 'false') . ")\n";
        ++$failed;
    }
}

echo "\n========== SUMMARY ==========\n";
echo "Passed: {$passed} Failed: {$failed}\n";
