<?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);

$type = 'wkl';

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

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

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

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

if ($id < 1) {
    echo json_encode(['success' => false, 'message' => 'Invalid ID']);
    http_response_code(400);
    exit;
}

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

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

$bezeichnung = $entrys[0]['programm'];


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

require_once $baseDir . '/../scripts/cache/cache-creators/regenerate-display-dependencies-cache.php';

generateIntersectCache($mysqli, $baseDir);


echo json_encode(['success' => true, 'message' => $bezeichnung.' gelöscht']);
http_response_code(200);
exit;