<?php

declare(strict_types=1);

namespace Autogedal\AwbRetry\Cron;

use Autogedal\AwbRetry\Model\Reconciliation;
use Psr\Log\LoggerInterface;

class Reconcile
{
    private const BATCH_SIZE = 100;

    public function __construct(
        private readonly Reconciliation $reconciliation,
        private readonly LoggerInterface $logger
    ) {
    }

    public function execute(): void
    {
        foreach ($this->reconciliation->getCandidates(self::BATCH_SIZE) as $candidate) {
            try {
                $this->reconciliation->resolve($candidate);
            } catch (\Throwable $throwable) {
                $this->logger->error(
                    'Automatic AWB retry log reconciliation failed.',
                    ['error_log_id' => (int) $candidate['entity_id'], 'exception' => $throwable]
                );
            }
        }
    }
}
