/** * Historical Lows REALES desde BD (ofertas_historial). * * IMPORTANTE: * - Esta consulta asume una estructura típica multi-tienda: * - ofertas: id, juego_id, tienda_id, ... * - ofertas_historial: oferta_id, precio (o price), created_at (o fecha), ... * - juegos: id, titulo, slug * - tiendas: id, nombre * * Si tus columnas difieren, ajusta SOLO los nombres dentro del SQL. * Si falla, devolvemos [] para no romper producción. */ function fetch_historical_lows(PDO $pdo, int $limit = 10): array { try { $sql = " SELECT j.slug, j.titulo, MIN(h.precio) AS low_price, MAX(h.created_at) AS last_seen, t.nombre AS tienda_nombre FROM ofertas_historial h INNER JOIN ofertas o ON o.id = h.oferta_id INNER JOIN juegos j ON j.id = o.juego_id INNER JOIN tiendas t ON t.id = o.tienda_id GROUP BY o.juego_id, j.slug, j.titulo, t.nombre ORDER BY low_price ASC LIMIT :lim "; $st = $pdo->prepare($sql); $st->bindValue(':lim', $limit, PDO::PARAM_INT); $st->execute(); $rows = $st->fetchAll(PDO::FETCH_ASSOC) ?: []; // Normalizamos mínimos $out = []; foreach ($rows as $r) { if (!is_array($r)) continue; $slug = (string)($r['slug'] ?? ''); $title = (string)($r['titulo'] ?? ''); if ($slug === '' || $title === '') continue; $out[] = [ 'slug' => $slug, 'title' => $title, 'low_price' => (float)($r['low_price'] ?? 0), 'last_seen' => (string)($r['last_seen'] ?? ''), 'store' => (string)($r['tienda_nombre'] ?? ''), ]; } return $out; } catch (\Throwable $e) { return []; } }