<?php
header('Content-Type: application/json');

if (!isset($baseDir)) {
	$baseDir = $_SERVER['DOCUMENT_ROOT'];
}

require_once $baseDir . '/../scripts/session_functions.php';

ini_wkvs_session();

check_user_permission('kampfrichter');

verify_csrf();

$gereatId = intval($_POST['geraetId'] ?? 0);

if ($gereatId < 1) {
    echo json_encode(['success' => false, 'message' => 'Kein Gerät angegeben']);
    exit;
}

$folder = realpath($baseDir . '/externe-geraete/json');
if ($folder === false) {
    echo json_encode([
        'success' => false,
        'message' => 'Could not find displays folder.'
    ]);
    exit;
}

$filename = 'audio-id-'. $gereatId .'.json';
$filepath = $folder . '/' . $filename;

if (!is_writable($folder)) {
    echo json_encode(['success' => false, 'message' => 'Folder not writable']);
    exit;
}

if (!is_file($filepath)){
    file_put_contents($filepath, []);
}

$json = [
    "start" => false
];

$jsonData = json_encode($json);

// Write file
if (file_put_contents($filepath, $jsonData) === false) {
    echo json_encode([
        'success' => false,
        'message' => 'Failed to write JSON file: ' . $filepath
    ]);
    exit;
}



// ---------- Return JSON ----------
echo json_encode([
    'success' => true,
    'message' => 'JSON updated successfully for '.$discipline,
    'disable_musik_button' => true
]);
exit;