<?php require_once __DIR__ . '/../includes/bootstrap.php';
header('Content-Type: application/json');
$limit = max(1, min(50, (int)($_GET['limit'] ?? 10)));
$sync = ugfx_chart_autoupdate(false);
$items = array_slice(ugfx_top50_all(), 0, $limit);
$meta = ugfx_chart_meta();
echo json_encode([
    'ok' => true,
    'items' => $items,
    'updated' => ugfx_top50_stats()['last_updated'] ?? '',
    'sync' => [
        'recorded' => (bool)($sync['recorded'] ?? false),
        'title' => (string)($sync['title'] ?? ($meta['last_seen_title'] ?? '')),
        'last_recorded_at' => (string)($meta['last_recorded_at'] ?? ''),
        'last_poll_at' => (string)($meta['last_poll_at'] ?? ''),
    ],
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
