<?php

declare(strict_types=1);

require_once dirname(__DIR__) . '/app/bootstrap.php';

use App\Repositories\ExecutiveActionRepository;

$hospital = strtolower(trim((string)($_GET['hospital'] ?? 'njch'))) ?: 'njch';
header('Content-Type: text/plain; charset=utf-8');

echo "Executive Alert Explainability Check
";
echo "Phase: 10.6 — Action Drilldown & Review Workflow
";
echo "Hospital: " . strtoupper($hospital) . "

";

try {
    $center = (new ExecutiveActionRepository($hospital))->center();
    $engine = is_array($center['engine'] ?? null) ? $center['engine'] : [];
    $system = is_array($center['system'] ?? null) ? $center['system'] : [];

    echo "Engine: " . (string)($engine['name'] ?? 'Executive Explainability') . "
";
    echo "Version: " . (string)($engine['version'] ?? 'Phase 10.6.1') . "
";
    echo "Status: " . (string)($system['status'] ?? 'UNKNOWN') . "
";
    echo "Last updated: " . (string)($center['last_updated'] ?? '-') . "

";

    foreach ((array)($center['actions'] ?? []) as $idx => $a) {
        echo "#" . (string)($idx + 1) . " " . strtoupper((string)($a['severity'] ?? 'routine')) . " | " . (string)($a['rule_id'] ?? '-') . "
";
        echo "Title: " . (string)($a['title'] ?? '-') . "
";
        echo "Observed: " . (string)($a['observed'] ?? '-') . "
";
        echo "Threshold: " . (string)($a['threshold'] ?? '-') . "
";
        echo "Confidence: " . (string)($a['confidence'] ?? '-') . "
";
        echo "Why: " . (string)($a['why'] ?? '-') . "
";
        echo "Recommendation: " . (string)($a['recommendation'] ?? '-') . "
";
        echo "Review source: " . (string)($a['review_source'] ?? '-') . "
";
        echo "Target: " . (string)($a['target'] ?? '-') . "

";
    }
} catch (Throwable $e) {
    echo "ERROR: " . $e->getMessage() . "
";
}
