<?php

require dirname(__DIR__) . '/prompts/sba_pe_emergency_batch_concepts.php';
require dirname(__DIR__) . '/validators/style_validator.php';

function assertHasCode(array $warnings, string $code, string $label): void
{
    foreach ($warnings as $w) {
        if (($w['code'] ?? '') === $code) {
            echo 'OK: ' . $label . PHP_EOL;

            return;
        }
    }
    echo 'FAIL: ' . $label . ' (missing code ' . $code . ')' . PHP_EOL;
    exit(1);
}

function assertNoCode(array $warnings, string $code, string $label): void
{
    foreach ($warnings as $w) {
        if (($w['code'] ?? '') === $code) {
            echo 'FAIL: ' . $label . ' (unexpected code ' . $code . ')' . PHP_EOL;
            exit(1);
        }
    }
    echo 'OK: ' . $label . PHP_EOL;
}

$base = [
    '_current_skill' => 'Emergency Management',
    '_condition_name' => 'Pulmonary embolism',
    'stem' => 'A 50 year old man has sudden breathlessness. Oxygen saturation 85% on breathing air. He is alert.',
    'lead_in' => 'What is the most appropriate immediate management?',
    'correct_answer_letter' => 'A',
    'option_a' => 'Administer alteplase thrombolysis',
    'option_b' => 'Start oxygen therapy',
    'option_c' => 'Give rivaroxaban',
    'option_d' => 'Arrange CT pulmonary angiogram',
    'option_e' => 'Monitor observations closely',
];

$w = sbaStyleWarningsPulmonaryEmbolismThrombolysisSeverity($base);
assertHasCode($w, 'pe_thrombolysis_without_instability', 'hypoxia-only stem with thrombolysis key warns');

$stableHypoxic = $base;
$stableHypoxic['option_a'] = 'Start oxygen therapy and give therapeutic anticoagulation';
$w2 = sbaStyleWarningsPulmonaryEmbolismThrombolysisSeverity($stableHypoxic);
assertNoCode($w2, 'pe_thrombolysis_without_instability', 'hypoxia-only with anticoagulation key does not warn thrombolysis');

$shocked = $base;
$shocked['stem'] = 'A 50 year old man has sudden breathlessness. Oxygen saturation 85% on breathing air. Blood pressure is 78/42 mmHg.';
$shocked['option_a'] = 'Administer oxygen and give alteplase thrombolysis';
$w3 = sbaStyleWarningsPulmonaryEmbolismThrombolysisSeverity($shocked);
assertNoCode($w3, 'pe_thrombolysis_without_instability', 'shocked hypoxic PE with thrombolysis does not warn');

$contra = $shocked;
$contra['stem'] = 'A 55 year old woman has massive pulmonary embolism with shock. Blood pressure is 80/50 mmHg. She had a recent intracranial haemorrhage.';
$contra['option_a'] = 'Administer alteplase thrombolysis';
$contra['_pe_emergency_scenario_plan'] = ['slot' => 'thrombolysis_contraindicated'];
$w4 = sbaStyleWarningsPeEmergencyClinicalAlignment($contra);
assertHasCode($w4, 'pe.emergency_thrombolysis_despite_contraindication', 'contraindicated stem with thrombolysis key warns');

$fairness = $shocked;
$fairness['lead_in'] = 'What is the most appropriate immediate management?';
$fairness['option_a'] = 'Start IV fluid resuscitation and administer alteplase thrombolysis';
$fairness['option_b'] = 'Administer oxygen and give alteplase thrombolysis';
$fairness['option_c'] = 'Give heparin alone';
$fairness['option_d'] = 'Arrange urgent critical care review without treatment';
$fairness['option_e'] = 'Delay thrombolysis and observe response';
$w5 = sbaStyleAcuteOxygenDistractorHypoxia($fairness, $fairness['stem']);
assertHasCode($w5, 'pe.emergency_oxygen_reperfusion_fairness', 'fluids-plus-thrombolysis key with O2+thrombolysis distractor warns');

echo PHP_EOL . 'All PE emergency validator tests passed.' . PHP_EOL;
