<?php

/**
 * Pulmonary embolism — Emergency Management bindings for the form-based SBA generator only.
 */

function sbaGeneratorConditionLooksPulmonaryEmbolism(string $conditionName): bool
{
    $c = mb_strtolower(trim($conditionName));

    return (bool) preg_match('/\bpulmonary\s+embol(?:ism|us)\b|\bpe\b/i', $c);
}

/**
 * @param array<string, mixed> $job
 */
function buildSbaGeneratorPulmonaryEmbolismEmergencyPromptBlock(array $job): string
{
    $condition = (string) ($job['condition_name'] ?? '');
    $skill = trim((string) ($job['skill'] ?? ''));
    if (!sbaGeneratorConditionLooksPulmonaryEmbolism($condition)
        || !preg_match('/^emergency\s+management$/i', $skill)) {
        return '';
    }

    if (!empty($job['_pe_emergency_scenario_plan']) && is_array($job['_pe_emergency_scenario_plan'])) {
        $lines = [];
        $lines[] = 'BINDING — PULMONARY EMBOLISM EMERGENCY MANAGEMENT (general rules; **this item** is governed by **BINDING — ASSIGNED PE EMERGENCY SCENARIO** above)';
        $lines[] = '**No sepsis logic** — no broad-spectrum IV antibiotics unless infection is clear in the stem.';
        $lines[] = '**Thrombolysis/reperfusion** only when the stem documents **haemodynamic instability** (systolic BP under 90 mmHg, shock, peri-arrest, obstructive collapse) — **not hypoxia alone**.';
        $lines[] = '**Hypoxic shocked PE:** key **oxygen plus urgent thrombolysis/reperfusion** — do not make “oxygen plus thrombolysis” a distractor when the key is fluids-plus-thrombolysis without oxygen.';
        $lines[] = '**Thrombolysis contraindicated:** key **urgent alternative reperfusion escalation** — never thrombolysis; avoid narrow surgical-versus-catheter keys unless explicit.';
        $lines[] = 'All options: homogeneous **emergency actions** — no CTPA/V-Q, oral anticoagulants, antibiotics, or monitor-only distractors in massive PE.';
        $lines[] = 'Observations in **MS AKT order** where used; **breathing air** not room air.';

        return "\n" . embedPromptField(implode("\n", $lines), 2500) . "\n";
    }

    $lines = [];
    $lines[] = 'BINDING — PULMONARY EMBOLISM EMERGENCY MANAGEMENT (Current skill = Emergency Management)';
    $lines[] = '**Batch planning:** for multiple Emergency items use **distinct PE scenario categories** before repeating (massive PE with shock; unstable PE needing reperfusion; thrombolysis contraindicated; peri-arrest PE; hypoxic PE with oxygen plus escalation). Vary stem setting, lead-in, key, and distractors.';
    $lines[] = '**Do not reuse sepsis emergency logic** — **no broad-spectrum IV antibiotics** unless the stem **clearly indicates infection**.';
    $lines[] = '';
    $lines[] = '**Severity matching (mandatory):**';
    $lines[] = '- **Thrombolysis / reperfusion** may be keyed **only** when the stem documents **haemodynamic instability** — e.g. **systolic BP under about 90 mmHg**, **shock**, **cardiac arrest**. **Hypoxia alone is not enough.**';
    $lines[] = '- **Hypoxic but stable** PE: key **oxygen plus anticoagulation / urgent escalation** — thrombolysis as distractor only unless instability is added.';
    $lines[] = '- **Massive PE with shock:** key **oxygen plus reperfusion/escalation** — not anticoagulation alone; not stable PE management.';
    $lines[] = '- **Thrombolysis contraindicated:** key **alternative reperfusion / critical care escalation** — do **not** key thrombolysis.';
    $lines[] = '';
    $lines[] = '**Lead-in:** use **definitive emergency / reperfusion** wording for shocked PE — vary across batch items.';
    $lines[] = '**Titles:** neutral labels (e.g. “Massive pulmonary embolism with shock”) — **not** “Management of …”.';
    $lines[] = '**Options:** homogeneous emergency actions; similar length; no repeated oxygen-only distractors; do not put thrombolysis in key and distractor without a clear distinction.';
    $lines[] = '**Do not** include **CTPA**, **V/Q scan**, or **D-dimer** as options when the lead-in asks for **immediate management**.';

    return "\n" . embedPromptField(implode("\n", $lines), 4500) . "\n";
}
