<?php
header('Content-Type: application/json');
require_once dirname(__DIR__) . '/include/config.php';

if (!isset($_GET['live'])) {
    $_vv_cached = vv_cache_read('monitor', 300);
    if ($_vv_cached !== null) { echo json_encode($_vv_cached); exit; }
    unset($_vv_cached);
}

require_once dirname(__DIR__) . '/include/monitor.php';
require_once dirname(__DIR__) . '/include/vms.php';
require_once dirname(__DIR__) . '/include/docker_folders.php';

// Pre-warm the API cache with one request (shared by all API-first functions below).
vv_api_data();

echo json_encode([
    'system'          => vv_system_info(),
    'fallback'        => vv_fallback_state(),
    'fallback_active' => vv_fallback_active(),
    'partner'         => vv_partner_state(),
    'resources'   => vv_system_resources(),
    'cpu'         => vv_cpu_per_core(),
    'mem'         => vv_memory_breakdown(),
    'net'         => vv_network_stats(),
    'gpu'         => vv_gpu_stats(),
    'gpu_procs'   => vv_gpu_processes(),
    'containers'  => vv_docker_containers(),
    'stopped'     => vv_docker_stopped(),
    'transcode'   => vv_transcode_sessions(),
    'ups'         => vv_ups_stats(),
    'parity'      => vv_parity_status(),
    'storage'     => vv_storage_pools(),
    'array_disks' => vv_array_disks(),
    'disk_io'     => vv_disk_io_rates(),
    'watchdog'       => vv_watchdog_summary(),
    'scripts'        => vv_scripts_status(),
    'rsync'          => vv_rsync_status(),
    'thresholds'     => vv_disk_thresholds(),
    'vms'            => vv_get_vms(),
    'docker_folders' => vv_get_docker_folders(),
    'remote_hosts'   => vv_remote_hosts_stats(),
    '_api_status'    => vv_api_get_status(),
    'ts'             => time(),
]);
