<?php

declare(strict_types=1);

require_once __DIR__ . '/../core/web_bootstrap.php';
require_once __DIR__ . '/../core/permissions.php';

/* ----------------------------------------------------------
   SESSION AUTH CHECK
---------------------------------------------------------- */

if (empty($_SESSION['user']) || empty($_SESSION['api_token'])) {
    header('Location: login.php');
    exit;
}

$user = $_SESSION['user'];

/* ----------------------------------------------------------
   RBAC CHECK
---------------------------------------------------------- */

if (!can($user, 'system_health', 'view', $pdo)) {
    http_response_code(403);
    die('Access denied.');
}

?>

<?php require 'partials/header.php'; ?>
<?php require 'partials/nav.php'; ?>

<h3>Real-Time System Health Monitor</h3>

<div class="row text-center mb-4">

    <div class="col-md-2">
        <div class="card shadow">
            <div class="card-body">
                <h6>Broadcasting</h6>
                <h3 id="broadcasting">-</h3>
            </div>
        </div>
    </div>

    <div class="col-md-2">
        <div class="card shadow">
            <div class="card-body">
                <h6>Enroute</h6>
                <h3 id="enroute">-</h3>
            </div>
        </div>
    </div>

    <div class="col-md-2">
        <div class="card shadow">
            <div class="card-body">
                <h6>In Progress</h6>
                <h3 id="in_progress">-</h3>
            </div>
        </div>
    </div>

    <div class="col-md-2">
        <div class="card shadow">
            <div class="card-body">
                <h6>Completed Today</h6>
                <h3 id="completed_today">-</h3>
            </div>
        </div>
    </div>

    <div class="col-md-2">
        <div class="card shadow">
            <div class="card-body">
                <h6>Active SP</h6>
                <h3 id="active_sp">-</h3>
            </div>
        </div>
    </div>

    <div class="col-md-2">
        <div class="card shadow">
            <div class="card-body">
                <h6>Active Fleet</h6>
                <h3 id="active_fleet">-</h3>
            </div>
        </div>
    </div>

</div>

<script>
const token = '<?= htmlspecialchars($_SESSION['api_token'], ENT_QUOTES, 'UTF-8') ?>';

function loadHealth() {

    fetch('/api/admin/get_system_health.php', {
        headers: { 'Authorization': 'Bearer ' + token }
    })
    .then(res => res.json())
    .then(data => {

        if (!data.success) return;

        const status = data.data.status_counts || {};

        document.getElementById('broadcasting').innerText =
            status.broadcasting ?? 0;

        document.getElementById('enroute').innerText =
            status.enroute ?? 0;

        document.getElementById('in_progress').innerText =
            status.in_progress ?? 0;

        document.getElementById('completed_today').innerText =
            data.data.completed_today ?? 0;

        document.getElementById('active_sp').innerText =
            data.data.active_sp ?? 0;

        document.getElementById('active_fleet').innerText =
            data.data.active_fleet ?? 0;
    });
}

/* Auto refresh every 15 seconds */
setInterval(loadHealth, 15000);
loadHealth();
</script>

<?php require 'partials/footer.php'; ?>