<?php

use MintyPHP\Module\Audit\AuditAuthorizationPolicy;
use MintyPHP\Module\Audit\Domain\SystemAuditOutcome;
use MintyPHP\Module\Audit\Service\SystemAuditService;
use MintyPHP\Module\Audit\Support\AuditPageHelper;
use MintyPHP\Router;
use MintyPHP\Support\Flash;

AuditPageHelper::requirePurgePost(
    AuditAuthorizationPolicy::ABILITY_SYSTEM_AUDIT_PURGE,
    'audit/system-audit',
    'system_audit'
);

$service = app(SystemAuditService::class);
$deleted = $service->purgeExpired();
$service->record('admin.system_audit.purge', SystemAuditOutcome::Success->value, [
    'metadata' => ['deleted_count' => $deleted],
]);

Flash::success(sprintf(t('%d system audit entries purged'), $deleted), 'audit/system-audit', 'system_audit_purged');
Router::redirect('audit/system-audit');
