<?php

declare(strict_types=1);

use ApiModel\Infrastructure\Queue\AsyncJobQueue;
use ApiModel\Infrastructure\Queue\JobDispatcher;

/** @var \Psr\Container\ContainerInterface $container */
$container = require dirname(__DIR__) . '/config/bootstrap.php';

/** @var AsyncJobQueue $queue */
$queue = $container->get(AsyncJobQueue::class);
/** @var JobDispatcher $dispatcher */
$dispatcher = $container->get(JobDispatcher::class);

$limit = 20;
foreach (($argv ?? []) as $arg) {
    if (str_starts_with((string) $arg, '--limit=')) {
        $limit = max(1, (int) substr((string) $arg, 8));
    }
}

$stats = $queue->processDue(
    static function (string $type, array $payload) use ($dispatcher): void {
        $dispatcher->dispatch($type, $payload);
    },
    $limit,
);

echo json_encode($stats, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR) . PHP_EOL;
