<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';

try {
    $pdo = db();
    // Obtenemos las 5 canciones con más likes que hayan sido actualizadas en los últimos 7 días
    $stmt = $pdo->prepare("
        SELECT song_title, likes_count 
        FROM song_likes 
        WHERE last_liked_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
        ORDER BY likes_count DESC 
        LIMIT 5
    ");
    $stmt->execute();
    $songs = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        'success' => true,
        'data' => $songs
    ]);
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'error' => $e->getMessage()
    ]);
}
