<?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('wk_leitung');

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

parse_input_to_delete();

verify_delete_csrf($_DELETE);

$id = (int) ($_DELETE['id'] ?? 0);

if ($id <= 0) {
    echo json_encode(['success' => false, 'message' => 'No valid ID']);
}

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


$type = 'wkl';

$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';

if ($dbconnection['success'] !== true){
    echo 'Critical DB Error.';
    exit;
}

$entrys = db_select($mysqli, $db_tabelle_wettkaempfe, "1", '`id` = ?', [$id]);

if (count($entrys) < 1) {
    echo json_encode(['success' => true, 'message' => 'Wettkampf bereits gelöscht']);
    http_response_code(410);
    exit;
}

db_delete($mysqli, $db_tabelle_wettkaempfe, ['id' => $id]);

echo json_encode(['success' => true, 'message' => 'Wettkampf gelöscht']);
http_response_code(200);