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

$user_name = $_GET['user_name'] ?? '';

if (empty($user_name)) {
    echo json_encode(['success' => false, 'error' => 'User name is required.']);
    exit;
}

try {
    // Get the latest unseen shoutout for this user
    $stmt = db()->prepare("SELECT id, message, dj_name FROM dj_shoutouts WHERE target_fan_name = ? AND is_seen = 0 ORDER BY created_at DESC LIMIT 1");
    $stmt->execute([$user_name]);
    $shoutout = $stmt->fetch();

    if ($shoutout) {
        // Mark as seen immediately so it doesn't pop up again
        $update = db()->prepare("UPDATE dj_shoutouts SET is_seen = 1 WHERE id = ?");
        $update->execute([$shoutout['id']]);
        
        echo json_encode(['success' => true, 'shoutout' => $shoutout]);
    } else {
        echo json_encode(['success' => true, 'shoutout' => null]);
    }
} catch (Exception $e) {
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
