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();
// 5. Top 10 Chat Interactors
$top_chatters = $db->query("
SELECT username, COUNT(*) as count
FROM messages
WHERE created_at > DATE_SUB(NOW(), INTERVAL 7 DAY)
AND username IS NOT NULL AND username != '' AND username != 'Anónimo'
GROUP BY username
ORDER BY count DESC
LIMIT 10
")->fetchAll();
$report_date = date('d/m/Y');
$week_start = date('d/m/Y', strtotime('-7 days'));
$html_content = "
";
foreach ($top_songs_requested as $s) {
$html_content .= "
" . htmlspecialchars($s['song']) . "
" . htmlspecialchars($s['artist']) . "
" . $s['count'] . "
";
}
$html_content .= "
⭐ Top Canciones (Más Likes)
Canción
Likes
";
foreach ($top_songs_liked as $s) {
$html_content .= "
" . htmlspecialchars($s['song_title']) . "
❤ " . $s['likes_count'] . "
";
}
$html_content .= "
";
if ($action === 'send') {
// Award points if not already awarded this week (optional safety, but let's keep it simple for now as requested for the automatic part)
// Actually, let's keep the reward logic in the cron as the "official" automated trigger.
// However, to be consistent, if they send it manually, we could also trigger it.
// Let's add the reward logic here too, but ONLY if it hasn't been run today.
$today = date('Y-m-d');
$stmt = $db->prepare("SELECT last_run FROM automation_logs WHERE task_name = 'weekly_report_rewards' AND DATE(last_run) = ?");
$stmt->execute([$today]);
if (!$stmt->fetch()) {
require_once __DIR__ . '/includes/points_helper.php';
$rewards = [50, 30, 20];
$rewarded_users = [];
foreach ($top_chatters as $i => $c) {
if ($i >= 3) break;
$username = $c['username'];
$points_to_add = $rewards[$i];
awardPoints($username, $points_to_add);
$rewarded_users[$username] = $points_to_add;
}
// Bot message to chat
if (!empty($rewarded_users)) {
$bot_msg = "🏆 ¡Atención comunidad! El Top 3 de interactores de esta semana ha sido premiado:\n\n";
$medals = ["🥇", "🥈", "🥉"];
$idx = 0;
foreach ($rewarded_users as $user => $pts) {
$bot_msg .= "{$medals[$idx]} $user: +$pts Fan Points\n";
$idx++;
}
$bot_msg .= "\n¡Sigue participando en el chat para ganar el próximo lunes! 📻✨";
$db->prepare("INSERT INTO messages (username, ip_address, message, type) VALUES (?, ?, ?, ?)")
->execute(['Lili Bot 🤖', '127.0.0.1', $bot_msg, 'text']);
}
$db->prepare("INSERT INTO automation_logs (task_name, last_run) VALUES ('weekly_report_rewards', NOW()) ON DUPLICATE KEY UPDATE last_run = NOW()")->execute();
}
$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 "