<?php

use MintyPHP\Router;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Auth\AuthServicesFactory;
use MintyPHP\Service\User\UserServicesFactory;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;

Guard::requireLogin();
Guard::requirePermissionOrForbidden(PermissionService::API_TOKENS_MANAGE);
$userAccountService = (new UserServicesFactory())->createUserAccountService();
$apiTokenService = (new AuthServicesFactory())->createApiTokenService();

$uuid = trim((string) ($id ?? ''));
$user = $uuid !== '' ? $userAccountService->findByUuid($uuid) : null;
if (!$user) {
    Router::redirect('admin/users');
}

$tokenId = (int) ($_POST['token_id'] ?? 0);

if ($tokenId > 0) {
    $apiTokenService->revoke($tokenId);
    Flash::success(t('API token revoked'), "admin/users/edit/{$uuid}", 'api_token_revoked');
}

Router::redirect("admin/users/edit/{$uuid}");
