<?php
declare(strict_types=1);
require_once __DIR__ . '/lib/page.php';
[$db, $config] = bpdh_boot();

$day = date('Y-m-d');
$rows = $db->fetchAll('SELECT platform, payload, updated_at FROM ys_hot_cache WHERE day=? ORDER BY platform', [$day]) ?: [];

// 若无缓存，尝试从旧 bpdh 同机 data 读取（迁移过渡）
if (!$rows) {
    $legacy = dirname(__DIR__) . '/../www.bpdh.cn_GCGmc/data/hot';
    // 无则展示说明
}

$html = '<div class="bpdh-card"><h1>全网热点</h1><p class="bpdh-muted">移植自 bpdh 热点采集；请配置 cron：php bin/collect_hot.php</p></div>';
if (!$rows) {
    $html .= '<div class="bpdh-card">今日暂无热点缓存。部署后执行 <code>php bin/collect_hot.php</code>。</div>';
}
foreach ($rows as $r) {
    $items = json_decode((string)$r['payload'], true);
    if (!is_array($items)) {
        continue;
    }
    $html .= '<div class="bpdh-card"><h2>' . bpdh_e($r['platform']) . '</h2><ol>';
    foreach (array_slice($items, 0, 30) as $it) {
        $title = is_array($it) ? (string)($it['title'] ?? $it['name'] ?? '') : (string)$it;
        $url = is_array($it) ? (string)($it['url'] ?? $it['link'] ?? '#') : '#';
        $html .= '<li style="margin:6px 0"><a href="' . bpdh_e($url) . '" target="_blank" rel="noopener">' . bpdh_e($title) . '</a></li>';
    }
    $html .= '</ol></div>';
}
bpdh_render('全网热点', $html, $config);
