<?php

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

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

ini_wkvs_session();

verify_csrf();

$accessPermission = trim($_POST['accessPermission'] ?? '');

$access = preg_replace("/[\W]/", "", trim($_POST['access'] ?? ''));
$accesstype = preg_replace("/[\W]/", "", trim($_POST['accesstype'] ?? ''));

if ($accessPermission === '' || $access === '' || $accesstype === '') {
    echo json_encode(['success' => false, 'message' => 'Parameters not correctly set']);
    http_response_code(400);
    exit;
}

require $baseDir . "/../scripts/websocket/ws-create-token.php";

if ($accessPermission === "R") {
    $token = generateWSReadToken($accesstype, $access);
} elseif ($accessPermission === "W") {
    check_multiple_allowed_permissions(['kampfrichter', 'wk_leitung']);
    $token = generateWSAdminToken($accesstype, $access);
} else {
    http_response_code(403);
    exit;
}

$responseBool = $token != null;

echo json_encode(['success' => $responseBool, 'token' => $token]);