<?php

use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Router;
use MintyPHP\Service\Access\SettingsAuthorizationPolicy;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;

$session = app(SessionStoreInterface::class)->all();
Guard::requireLogin();

$currentUserId = (int) ($session['user']['id'] ?? 0);
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
$decision = $authorizationService->authorize(SettingsAuthorizationPolicy::ABILITY_ADMIN_SETTINGS_TOKENS_REVOKE, [
    'actor_user_id' => $currentUserId,
]);
if (!$decision->isAllowed()) {
    Guard::deny();
}

if (!actionRequirePost('admin/settings/api')) {
    return;
}

if (!actionRequireCsrf('admin/settings/api', 'admin/settings/api', 'settings_tokens_revoke')) {
    return;
}

$apiTokenService = app(\MintyPHP\Service\Auth\ApiTokenService::class);
$count = $apiTokenService->revokeAllByAdmin();
Flash::success(sprintf(t('%d API tokens revoked'), $count), 'admin/settings/api', 'api_tokens_revoked');
Router::redirect('admin/settings/api');
