<?php
require_once __DIR__ . '/../auth.php';
if (function_exists('require_login')) require_login('../login.php');
require_once __DIR__ . '/../db_bootstrap.php';
require_once __DIR__ . '/../receiver_subscriber_match_lib.php';
if (function_exists('ensure_db')) ensure_db(true);
$conn = $GLOBALS['conn'] ?? null;
header('Content-Type: application/json; charset=utf-8');
if (!($conn instanceof mysqli)) { echo json_encode(['ok'=>false,'message'=>'db not ready'], JSON_UNESCAPED_UNICODE); exit; }
$ttl = max(30, min(900, (int)($_GET['ttl'] ?? 300)));
// This endpoint reads existing receiver probe caches and subscriber/PPP caches only. It does not poll receivers or MikroTik.
$snap = function_exists('ua_receiver_match_snapshot') ? ua_receiver_match_snapshot($conn, $ttl, false) : ['ok'=>false,'items'=>[],'summary'=>[]];
$items = [];
foreach ((array)($snap['items'] ?? []) as $id => $item) {
    $items[] = [
        'device_id' => (int)($item['device_id'] ?? $id),
        'name' => (string)($item['name'] ?? ''),
        'host' => (string)($item['host'] ?? ''),
        'router_name' => (string)($item['router_name'] ?? ''),
        'interface' => (string)($item['interface'] ?? ''),
        'interface_label' => (string)($item['interface_label'] ?? ''),
        'device_type' => (string)($item['device_type'] ?? ''),
        'source_type' => (string)($item['source_type'] ?? 'receiver'),
        'sector_id' => (int)($item['sector_id'] ?? 0),
        'sector_name' => (string)($item['sector_name'] ?? ''),
        'sector_vlan_id' => (int)($item['sector_vlan_id'] ?? 0),
        'sector_match_method' => (string)($item['sector_match_method'] ?? ''),
        'parent_device_id' => (int)($item['parent_device_id'] ?? 0),
        'topology_depth' => (int)($item['topology_depth'] ?? 0),
        'raw_mac_count' => (int)($item['raw_mac_count'] ?? 0),
        'raw_bridge_count' => (int)($item['raw_bridge_count'] ?? 0),
        'matched_count' => (int)($item['matched_count'] ?? 0),
        'unknown_count' => (int)($item['unknown_count'] ?? 0),
        'ambiguous_count' => (int)($item['ambiguous_count'] ?? 0),
        'conflict_preview' => (string)($item['conflict_preview'] ?? ''),
        'conflict_details' => array_slice((array)($item['conflict_details'] ?? []), 0, 20),
        'topology_shadow_count' => (int)($item['topology_shadow_count'] ?? 0),
        'cache_age' => (int)($item['cache_age'] ?? 0),
        'source' => (string)($item['source'] ?? ''),
    ];
}
echo json_encode([
    'ok' => !empty($snap['ok']),
    'ts' => (int)($snap['ts'] ?? 0),
    'age' => (int)($snap['age'] ?? 0),
    'source' => (string)($snap['source'] ?? ''),
    'summary' => (array)($snap['summary'] ?? []),
    'receiver_summary' => (array)($snap['receiver_summary'] ?? $snap['summary'] ?? []),
    'router_port_summary' => (array)($snap['router_port_summary'] ?? []),
    'combined_summary' => (array)($snap['combined_summary'] ?? []),
    'stale_cleanup' => (array)($snap['stale_cleanup'] ?? []),
    'items' => $items,
], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
