<?php

use MintyPHP\Router;
use MintyPHP\Session;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Audit\AuditServicesFactory;

Guard::requireLogin();
Guard::requirePermission(PermissionService::SETTINGS_UPDATE);

if (strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) !== 'POST') {
    Router::redirect('admin/api-audit');
}
if (!Session::checkCsrfToken()) {
    Flash::error(t('Form expired, please try again'), 'admin/api-audit', 'api_audit_csrf');
    Router::redirect('admin/api-audit');
}

$factory = new AuditServicesFactory();
$deleted = $factory->createApiAuditService()->purgeExpired();
Flash::success(sprintf(t('%d API audit entries purged'), $deleted), 'admin/api-audit', 'api_audit_purged');
Router::redirect('admin/api-audit');
