<?php
// admin/roulette_asset_toggle_mute.php
// Alterna is_muted (0/1) para um asset.

require __DIR__ . '/../db/config.php';          // $pdo (PDO)

$asset_id   = isset($_POST['asset_id'])   ? (int)$_POST['asset_id']   : 0;
$roulette_id= isset($_POST['roulette_id'])? (int)$_POST['roulette_id']: 0;
$muted      = isset($_POST['muted'])      ? (int)$_POST['muted']      : 0;
$muted      = $muted ? 1 : 0;

if ($asset_id <= 0) {
  http_response_code(400);
  echo "asset_id inválido";
  exit;
}

try {
  $st = $pdo->prepare("UPDATE roulette_assets SET is_muted = :muted WHERE id = :id");
  $st->execute([":muted" => $muted, ":id" => $asset_id]);
} catch (Throwable $e) {
  http_response_code(500);
  echo "Erro DB: " . $e->getMessage();
  exit;
}

// Volta para o card de áudios, na roleta certa
$target = "nova_roleta.php";
if ($roulette_id > 0) { $target .= "?id=" . $roulette_id . "#audios"; }
header("Location: " . $target);
exit;
