<?php

use MintyPHP\Router;
use MintyPHP\Session;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;

Guard::requireLogin();
Guard::requireAbility(\MintyPHP\Service\Access\SettingsAuthorizationPolicy::ABILITY_ADMIN_SETTINGS_UPDATE);

if (strtoupper((string) (requestInput()->method())) !== 'POST') {
    Router::redirect('admin/user-lifecycle-audit');
}
$errorBag = formErrors();
if (!Session::checkCsrfToken()) {
    $errorBag->addGlobal(t('Form expired, please try again'));
    flashFormErrors($errorBag, 'admin/user-lifecycle-audit', 'user_lifecycle_audit');
    Router::redirect('admin/user-lifecycle-audit');
}

$deleted = app(\MintyPHP\Service\Audit\UserLifecycleAuditService::class)->purgeExpired();
Flash::success(
    sprintf(t('%d user lifecycle audit entries purged'), $deleted),
    'admin/user-lifecycle-audit',
    'user_lifecycle_audit_purged'
);
Router::redirect('admin/user-lifecycle-audit');
