<?php
// admin/api/speed_gauges.php
// Thin JSON endpoint. Data logic lives in ../speed_gauges_lib.php as the single source.

if (session_status() === PHP_SESSION_NONE) { @session_start(); }
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/api_common.php';


$ua_logged = !empty($_SESSION['admin_id']) || !empty($_SESSION['admin_user_id']) || !empty($_SESSION['admin_user']);
if (!$ua_logged) {
  out(['status'=>'offline','ok'=>false,'message'=>'Unauthorized'], 401);
}

try {
  require_once __DIR__ . '/../speed_gauges_lib.php';
  out(ua_speed_gauges_payload());
} catch (Throwable $e) {
  if (function_exists('ua_speed_default_payload')) {
    out(ua_speed_default_payload());
  }
  out([
    'status'=>'offline',
    'rx_bps'=>0,'tx_bps'=>0,'consumed_bps'=>0,
    'rx_gb'=>0,'tx_gb'=>0,
    'iface'=>null,
    'message'=>'لا يوجد اتصال MikroTik — العدادات ثابتة على 0'
  ]);
}
