<?php

declare(strict_types=1);

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

use App\Repositories\ExecutiveSummaryRepository;

header('Content-Type: text/plain; charset=utf-8');

$hospital = strtolower((string)($_GET['hospital'] ?? 'njch'));
$repo = new ExecutiveSummaryRepository($hospital);

try {
    $info = $repo->operationalDayInfo();

    echo "Operational Day Check\n";
    echo "Hospital: " . strtoupper($hospital) . "\n";
    echo "EXECUTIVE_OPERATIONAL_DAY_START_HOUR: " . (string)env('EXECUTIVE_OPERATIONAL_DAY_START_HOUR', '7') . "\n\n";
    echo "Current DB time: " . (string)($info['current_time'] ?? '-') . "\n";
    echo "Operational date: " . (string)($info['operational_date'] ?? '-') . "\n";
    echo "Window start: " . (string)($info['window_start'] ?? '-') . "\n";
    echo "Window end: " . (string)($info['window_end'] ?? '-') . "\n";
    echo "Night rollover active: " . (!empty($info['is_rollover']) ? 'YES' : 'NO') . "\n\n";

    echo "Policy:\n";
    echo "- Before the configured start hour, daily KPIs use the previous operational day.\n";
    echo "- At/after the configured start hour, daily KPIs use the new operational day.\n";
} catch (Throwable $e) {
    http_response_code(500);
    echo "ERROR: " . $e->getMessage() . "\n";
}
