<?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 & Recommendations Check\n";
echo "Hospital: " . strtoupper($hospital) . "\n\n";

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

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

    echo "Thresholds from .env/defaults:\n";
    foreach ($thresholds as $k => $v) {
        echo str_pad((string)$k, 36) . " | " . (string)$v . "\n";
    }

    echo "\nCurrent metrics used by rules:\n";
    foreach (['opd','er','admissions','discharges','scheduled_operations','performed_operations','today_total','month_total','total_beds','occupied_beds','available_beds','occupancy_rate','blocked_beds'] as $k) {
        $v = array_key_exists($k, $metrics) && $metrics[$k] !== null ? (string)$metrics[$k] : 'NULL';
        echo str_pad($k, 28) . " | " . $v . "\n";
    }

    echo "
Revenue pace calibration:
";
    $cal = is_array($engine['revenue_calibration'] ?? null) ? $engine['revenue_calibration'] : [];
    foreach (['operational_date','window_start','now','elapsed_hours','clock_hour','patient_flow','daily_pace','time_adjusted_expected','watch_floor','today_total','check_due','skip_reason'] as $k) {
        $v = array_key_exists($k, $cal) ? $cal[$k] : '';
        if (is_bool($v)) { $v = $v ? 'YES' : 'NO'; }
        if (is_float($v)) { $v = number_format($v, 2, '.', ''); }
        echo str_pad($k, 28) . " | " . (string)$v . "
";
    }

    echo "\nCounts:\n";
    foreach (['critical','watch','routine','opportunity'] as $k) {
        echo str_pad($k, 14) . " | " . (string)($counts[$k] ?? 0) . "\n";
    }

    echo "\nTriggered rules/actions:\n";
    foreach ((array)($center['actions'] ?? []) as $a) {
        echo "- " . strtoupper((string)($a['severity'] ?? 'routine')) . " | " . (string)($a['rule_id'] ?? '-') . "\n";
        echo "  Title: " . (string)($a['title'] ?? '-') . "\n";
        echo "  Observed: " . (string)($a['observed'] ?? '-') . "\n";
        echo "  Threshold: " . (string)($a['threshold'] ?? '-') . "\n";
        echo "  Target: " . (string)($a['target'] ?? '-') . "\n";
    }

    echo "\n.env override examples:\n";
    echo "EXEC_ALERT_OCCUPANCY_WATCH=85\n";
    echo "EXEC_ALERT_OCCUPANCY_CRITICAL=95\n";
    echo "EXEC_ALERT_AVAILABLE_BEDS_WATCH=15\n";
    echo "EXEC_ALERT_AVAILABLE_BEDS_CRITICAL=5\n";
    echo "EXEC_ALERT_ER_WATCH=35\n";
    echo "EXEC_ALERT_ER_CRITICAL=60\n";
    echo "EXEC_ALERT_REVENUE_PACE_RATIO_WATCH=0.25\n";
echo "EXEC_ALERT_REVENUE_PACE_MIN_ELAPSED_HOURS=6\n";
echo "EXEC_ALERT_REVENUE_PACE_MIN_FLOW=50\n";
echo "EXEC_ALERT_REVENUE_PACE_TIME_WEIGHT_FLOOR=0.12\n";
} catch (Throwable $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
}
