<?php

use MintyPHP\Router;
use MintyPHP\Service\Audit\ImportAuditService;
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/import-audit');
}
$errorBag = formErrors();
if (!Session::checkCsrfToken()) {
    $errorBag->addGlobal(t('Form expired, please try again'));
    flashFormErrors($errorBag, 'admin/import-audit', 'import_audit');
    Router::redirect('admin/import-audit');
}

$deleted = app(ImportAuditService::class)->purgeExpired();
Flash::success(sprintf(t('%d import audit entries purged'), $deleted), 'admin/import-audit', 'import_audit_purged');
Router::redirect('admin/import-audit');
