<?php

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

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

ini_wkvs_session();

verify_csrf();

$new_value = $_POST['freigabe'] ?? '';

$type = $_POST['type'] ?? 'nan';

$allowedTypes = ['kampfrichter', 'trainer'];

if (in_array($type, $allowedTypes)) {
    check_user_permission($type);
} else {
    echo json_encode(['success' => false, 'message' => 'no permissions']);
    exit;
}

if (!$new_value) {
	echo json_encode('Invalid Input');
	exit;
}

if ($type === 'kampfrichter'){
	$_SESSION['selectedFreigabeIdKampfrichter'] = $new_value;
}

if ($type === 'trainer'){
	$_SESSION['selectedFreigabeTrainer'] = $new_value;
}

// ---------- Return JSON ----------
echo json_encode(['success' => true, 'message' => 'SESSION updated']);
exit;