<?php

use MintyPHP\Router;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Scheduler\SchedulerServicesFactory;
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/scheduled-jobs');
}
if (!Session::checkCsrfToken()) {
    Flash::error(t('Form expired, please try again'), 'admin/scheduled-jobs', 'scheduled_job_purge_csrf');
    Router::redirect('admin/scheduled-jobs');
}

$factory = new SchedulerServicesFactory();
$deleted = $factory->createScheduledJobService()->purgeRunsExpired();
Flash::success(sprintf(t('%d scheduler run logs purged'), $deleted), 'admin/scheduled-jobs', 'scheduled_job_runs_purged');
Router::redirect('admin/scheduled-jobs');
