<?php
require_once '../../config/db.php';

header('Content-Type: application/json');

try {
    $sql = "SELECT s.*, u.username, u.avatar 
            FROM streams s 
            JOIN users u ON s.user_id = u.id 
            WHERE s.is_live = TRUE 
            ORDER BY s.viewers DESC, s.started_at DESC LIMIT 20";
            
    $streams = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        'count' => count($streams),
        'data' => $streams
    ]);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
?>
