<?php
// api/live-streams/update.php
require_once '../../config/cors.php';
require_once '../../config/db.php';
require_once '../../helpers/response.php';
require_once '../../helpers/auth.php';

$admin = requireAdmin();
$data = json_decode(file_get_contents('php://input'), true);

$id = $data['id'] ?? null;
$title = $data['title'] ?? null;
$description = $data['description'] ?? null;
$scheduled_start = $data['scheduled_start'] ?? null;
$chat_enabled = isset($data['chat_enabled']) ? ($data['chat_enabled'] ? 1 : 0) : null;

if (!$id) sendError('ID required', 400);

$updates = [];
$params = [':id' => $id];

if ($title !== null) {
    $updates[] = "title = :title";
    $params[':title'] = $title;
}
if ($description !== null) {
    $updates[] = "description = :description";
    $params[':description'] = $description;
}
if ($scheduled_start !== null) {
    $updates[] = "scheduled_start = :scheduled_start";
    $params[':scheduled_start'] = $scheduled_start;
}
if ($chat_enabled !== null) {
    $updates[] = "chat_enabled = :chat_enabled";
    $params[':chat_enabled'] = $chat_enabled;
}

if (empty($updates)) {
    sendError('No fields to update', 400);
}

$sql = "UPDATE live_streams SET " . implode(', ', $updates) . " WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);

sendSuccess(null, 'Broadcast updated');
?>