<?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/user-lifecycle-audit');
}
if (!Session::checkCsrfToken()) {
    Flash::error(t('Form expired, please try again'), 'admin/user-lifecycle-audit', 'user_lifecycle_audit_csrf');
    Router::redirect('admin/user-lifecycle-audit');
}

$factory = new AuditServicesFactory();
$deleted = $factory->createUserLifecycleAuditService()->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');
