<?php

namespace QUI\MasterLogin;

use QUI\Cron\Manager as CronManager;
use QUI\System\Log;

use function is_numeric;

class Cron
{
    /**
     * Remove Master Login audit entries older than the configured number of days.
     *
     * @param array<string, mixed> $params
     */
    public static function purgeLog(array $params, CronManager $CronManager): void
    {
        $maxAge = 90;

        if (isset($params['max_age']) && is_numeric($params['max_age'])) {
            $configuredMaxAge = (int)$params['max_age'];

            if ($configuredMaxAge > 0) {
                $maxAge = $configuredMaxAge;
            }
        }

        try {
            Manager::deleteLogEntriesOlderThanDays($maxAge);
        } catch (\Throwable $Exception) {
            Log::writeException($Exception);
        }
    }
}
