<?php

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

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

ini_wkvs_session();

check_user_permission('trainer');

verify_csrf();

if (!isset($_POST['oldMusicId'])) {
    echo json_encode(['success' => false]);
    http_response_code(422);
    exit;
}

$oldMusicId = intval($_POST['oldMusicId']);

if ($oldMusicId < 1) {
    echo json_encode(['success' => true]);
    http_response_code(202);
    exit;
}

$type = 'tr';

$data = include $baseDir . '/../scripts/db/db-verbindung-script.php';

if ($data['success'] === false){
	echo json_encode(['success' => false, 'message' => $data['message']]);
    http_response_code(500);
    exit; 
}

require $baseDir . '/../scripts/db/db-tables.php';

$sql = "DELETE $db_tabelle_audiofiles WHERE id = ?";

$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i", $oldMusicId);

if (!$stmt->execute()) {
    http_response_code(500);
    exit;
}

$stmt->close();
$mysqli->close();

echo json_encode(['success' => true]);
http_response_code(200);
exit;