<?php
/**
 * Radio Toggle State — Save/get per-user Broadcastify preference.
 */
require_once __DIR__ . '/../config/auth.php';
$user = requireLogin();

$db = getDB();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    validateCsrf();
    $enabled = ($_POST['enabled'] ?? '0') === '1' ? 1 : 0;
    $db->prepare('UPDATE users SET radio_enabled = :val WHERE id = :id')
       ->execute([':val' => $enabled, ':id' => $user['id']]);
    $_SESSION['user']['radio_enabled'] = (bool)$enabled;
    jsonResponse(['success' => true, 'radio_enabled' => $enabled]);
} else {
    $stmt = $db->prepare('SELECT radio_enabled FROM users WHERE id = :id');
    $stmt->execute([':id' => $user['id']]);
    $row = $stmt->fetch();
    jsonResponse(['radio_enabled' => (bool)($row['radio_enabled'] ?? false)]);
}
