<?php
header('Content-Type: application/json');
error_reporting(0);
ini_set('display_errors', 0);

require_once '../session_check.php';

// =======================
// 🔧 EXEMPLO DE DADOS (SUBSTITUA PELO BANCO)
// =======================

// SIMULAÇÃO de logs (substituir por SQL depois)
$data = [
    [
        "mac" => "AA:BB:CC:11:22:33",
        "content_name" => "Canal Esportes HD",
        "content_type" => "live",
        "device" => "Smart TV",
        "online_seconds" => 320
    ],
    [
        "mac" => "DD:EE:FF:44:55:66",
        "content_name" => "Filme Matrix",
        "content_type" => "movie",
        "device" => "Android Box",
        "online_seconds" => 1250
    ],
    [
        "mac" => "11:22:33:AA:BB:CC",
        "content_name" => "Série Exemplo",
        "content_type" => "series",
        "device" => "Fire Stick",
        "online_seconds" => 5400
    ]
];

// =======================
// 📊 SUMMARY
// =======================
$summary = [
    "online" => count($data),
    "live" => 0,
    "movie" => 0,
    "series" => 0
];

foreach ($data as $item) {
    if (!isset($item['content_type'])) continue;

    switch ($item['content_type']) {
        case 'live':
            $summary['live']++;
            break;
        case 'movie':
            $summary['movie']++;
            break;
        case 'series':
            $summary['series']++;
            break;
    }
}

// =======================
// 📱 DEVICES SUMMARY
// =======================
$devices = [];

foreach ($data as $item) {
    $dev = $item['device'] ?? 'Unknown';

    if (!isset($devices[$dev])) {
        $devices[$dev] = 0;
    }

    $devices[$dev]++;
}

$devices_out = [];
foreach ($devices as $name => $count) {
    $devices_out[] = [
        "name" => $name,
        "count" => $count
    ];
}

// =======================
// 🚀 OUTPUT FINAL (CRÍTICO)
// =======================
echo json_encode([
    "success" => true,
    "data" => $data,
    "summary" => $summary,
    "devices" => $devices_out
], JSON_UNESCAPED_UNICODE);

exit;