<?php
declare(strict_types=1);

use App\Core\Application;
use App\Modules\Cron\CronHandlerFactory;
use App\Modules\Cron\CronRepository;

/** @var Application $app */
$app = require dirname(__DIR__) . '/bootstrap/app.php';

$limit = 20;
foreach (array_slice($argv, 1) as $arg) {
    if (preg_match('/^--limit=(\d+)$/', (string) $arg, $matches) === 1) {
        $limit = max(1, min(100, (int) ($matches[1] ?? 20)));
    }
}

$cronRepository = new CronRepository($app->db());
$factory = new CronHandlerFactory(
    $app->db(),
    (string) $app->config('app.integrations.secret', ''),
    $app->basePath()
);
$runner = $factory->build($cronRepository, $app->logger());

$result = $runner->run($limit);
fwrite(STDOUT, json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL);
