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

try {
    $pdo = db();
    // Fetch tips from the last 15 seconds to trigger celebrations
    $stmt = $pdo->prepare("SELECT * FROM dj_tips WHERE created_at > (NOW() - INTERVAL 15 SECOND) ORDER BY created_at DESC LIMIT 5");
    $stmt->execute();
    $tips = $stmt->fetchAll();

    // Fetch recent thanks from the last 15 seconds
    $stmt = $pdo->prepare("SELECT * FROM dj_tips WHERE thanked_at > (NOW() - INTERVAL 15 SECOND) ORDER BY thanked_at DESC LIMIT 5");
    $stmt->execute();
    $thanks = $stmt->fetchAll();

    // Also get current DJ
    $stmt = $pdo->query("SELECT setting_value FROM settings WHERE setting_key = 'current_dj'");
    $dj = $stmt->fetchColumn() ?: "Lili";

    echo json_encode([
        "success" => true,
        "tips" => $tips,
        "thanks" => $thanks,
        "current_dj" => $dj
    ]);
} catch (Exception $e) {
    echo json_encode(["success" => false, "error" => $e->getMessage()]);
}
