<?php

use MintyPHP\Router;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Import\ImportServicesFactory;
use MintyPHP\Session;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;

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

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

$factory = new ImportServicesFactory();
$deleted = $factory->createImportAuditService()->purgeExpired();
Flash::success(sprintf(t('%d import audit entries purged'), $deleted), 'admin/import-audit', 'import_audit_purged');
Router::redirect('admin/import-audit');
