<?php
header('Content-Type: application/json');

$networksFile = __DIR__ . '/networks.json';
$jsonDir      = __DIR__ . '/json/';

if (!file_exists($networksFile)) {
    echo json_encode(['error' => 'networks.json not found']);
    exit;
}

$networksData = json_decode(file_get_contents($networksFile), true);
if (!isset($networksData['files']) || !is_array($networksData['files'])) {
    echo json_encode(['error' => 'Invalid networks.json format']);
    exit;
}

$allChannels = [];

foreach ($networksData['files'] as $fileName) {
    $path = $jsonDir . $fileName;
    if (!file_exists($path)) continue;

    $data = json_decode(file_get_contents($path), true);
    if (!$data) continue;

    // You can decide your schema; here I just merge
    $allChannels[] = [
        'file' => $fileName,
        'data' => $data
    ];
}

echo json_encode([
    'status'   => 'ok',
    'networks' => $allChannels
]);