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

try {
    $pdo = db();
    // Obtener el Top 5 de usuarios que más canciones han pedido en los últimos 7 días
    $stmt = $pdo->query("
        SELECT requester, COUNT(*) as total_requests 
        FROM song_requests 
        WHERE requester IS NOT NULL AND requester != '' AND requester != 'Anónimo'
        AND created_at > DATE_SUB(NOW(), INTERVAL 7 DAY)
        GROUP BY requester 
        ORDER BY total_requests DESC 
        LIMIT 5
    ");
    $top_requesters = $stmt->fetchAll(PDO::FETCH_ASSOC);

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