<?php

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

function assertEq(mixed $a, mixed $b, string $label): void
{
    if ($a !== $b) {
        echo 'FAIL: ' . $label . ' (got ' . var_export($a, true) . ', want ' . var_export($b, true) . ')' . PHP_EOL;
        exit(1);
    }
    echo 'OK: ' . $label . PHP_EOL;
}

$jobBase = [
    'condition_name' => 'Pulmonary embolism',
    'presentation' => 'Massive pulmonary embolism with shock',
    'skill' => 'Emergency Management',
    '_job_skills' => 'Emergency Management',
    'tutor_comments' => 'Focus on massive PE with shock requiring urgent reperfusion — not stable community PE.',
    'num_questions' => 1,
    'questions_completed' => 0,
    '_batch_prior_questions' => [],
];

$one = sbaPeBuildFullEmergencyJobScenarioPlan($jobBase);
assertEq(count($one), 1, '1-question job has one plan');
assertEq($one[1]['slot'], 'initial_massive_pe_shock', '1Q massive PE picks initial shock slot');

$job3 = $jobBase;
$job3['num_questions'] = 3;
$job3['questions_completed'] = 0;
$plan3 = sbaPeBuildFullEmergencyJobScenarioPlan($job3);
$slots3 = array_values(array_map(static fn ($item) => $item['slot'], $plan3));
assertEq(count(array_unique($slots3)), 3, '3-question batch has 3 distinct slots');
assertEq(
    $slots3,
    ['initial_massive_pe_shock', 'unstable_pe_reperfusion_required', 'thrombolysis_contraindicated'],
    '3Q massive shock uses core curriculum order'
);
assertEq(
    sbaPePlannedBatchSlotSequence(3, $jobBase['presentation'], $jobBase['tutor_comments']),
    ['initial_massive_pe_shock', 'unstable_pe_reperfusion_required', 'thrombolysis_contraindicated'],
    'planned sequence for 3Q massive shock'
);

$hypoxicStem = 'A 60 year old woman has breathlessness. Oxygen saturation 88% on air. Blood pressure 78/40 mmHg.';
assertEq(
    sbaPeInferEmergencyScenarioCategory($hypoxicStem, 'Most appropriate emergency treatment?', 'Administer oxygen and give thrombolysis'),
    'initial_massive_pe_shock',
    'shock with hypoxia infers massive PE not hypoxic-only slot'
);

$jobContra = $jobBase;
$jobContra['presentation'] = 'Massive PE after recent surgery';
$jobContra['tutor_comments'] = 'Thrombolysis contraindicated due to recent major surgery';
$planContra = sbaPeBuildFullEmergencyJobScenarioPlan($jobContra);
assertEq($planContra[1]['slot'], 'thrombolysis_contraindicated', 'Contraindication presentation picks contraindicated slot');

$q = [
    '_current_skill' => 'Emergency Management',
    '_condition_name' => 'Pulmonary embolism',
    '_tutor_comments' => 'Focus on massive PE with shock requiring urgent reperfusion',
    'stem' => 'A 55 year old man has sudden breathlessness. His blood pressure is 78/42 mmHg and oxygen saturation 88% on breathing air.',
];
$stableWarnings = sbaStyleWarningsStableCommunityInfectionPrescribing($q);
$hasStableMismatch = false;
foreach ($stableWarnings as $w) {
    if (($w['code'] ?? '') === 'prescribing.stable_community_tutor_mismatch_acute_stem_features') {
        $hasStableMismatch = true;
    }
}
assertEq($hasStableMismatch, false, 'massive PE tutor comments do not trigger stable-community mismatch');

echo PHP_EOL . 'All PE scenario plan tests passed.' . PHP_EOL;
