DATE_SUB(NOW(), INTERVAL 7 DAY) "; $totals = $db->query($stats_query)->fetch(); // 2. Top 10 Requesters $top_requesters = $db->query(" SELECT requester, COUNT(*) as count FROM song_requests WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) AND requester IS NOT NULL AND requester != '' AND requester != 'Anónimo' GROUP BY requester ORDER BY count DESC LIMIT 10 ")->fetchAll(); // 3. Top 10 Songs (by requests) $top_songs_requested = $db->query(" SELECT artist, song, COUNT(*) as count FROM song_requests WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY) GROUP BY artist, song ORDER BY count DESC LIMIT 10 ")->fetchAll(); // 4. Top 10 Songs (by likes) $top_songs_liked = $db->query(" SELECT song_title, likes_count FROM song_likes WHERE last_liked_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) ORDER BY likes_count DESC LIMIT 10 ")->fetchAll(); $report_date = date('d/m/Y'); $week_start = date('d/m/Y', strtotime('-7 days')); $html_content = "

Reporte Semanal Lili Records Radio

Periodo: $week_start al $report_date

Total Peticiones

{$totals['total_requests']}

Vía WhatsApp

{$totals['whatsapp_requests']}

Vía Web

{$totals['web_requests']}

🏆 Top 10 Oyentes de la Semana

"; foreach ($top_requesters as $i => $r) { $crown = ($i === 0) ? "👑 " : ""; $html_content .= " "; } $html_content .= "
# Oyente Peticiones
" . ($i + 1) . " $crown" . htmlspecialchars($r['requester']) . " " . $r['count'] . "

🎵 Top Canciones (Más Pedidas)

"; foreach ($top_songs_requested as $s) { $html_content .= " "; } $html_content .= "
Canción Artista Veces
" . htmlspecialchars($s['song']) . " " . htmlspecialchars($s['artist']) . " " . $s['count'] . "

⭐ Top Canciones (Más Likes)

"; foreach ($top_songs_liked as $s) { $html_content .= " "; } $html_content .= "
Canción Likes
" . htmlspecialchars($s['song_title']) . " " . $s['likes_count'] . "
"; if ($action === 'send') { $subject = "📊 Reporte de Actividad Semanal - Lili Records Radio ($report_date)"; $res = MailService::sendMail(null, $subject, $html_content); header('Content-Type: application/json'); echo json_encode($res); exit; } // Default action: preview echo $html_content; if ($action === 'preview') { echo "
"; }