<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Scheduler;

use CpCreation\VitiCore\Versioning\EcommerceVersionRecorder;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final readonly class PublishDueBusinessVersionsMessageHandler
{
    public function __construct(
        private EcommerceVersionRecorder $ecommerceVersionRecorder,
        private ?LoggerInterface $logger = null,
    ) {
    }

    public function __invoke(PublishDueBusinessVersionsMessage $message): void
    {
        $publishedCount = $this->ecommerceVersionRecorder->publishDueScheduledChanges(
            new \DateTimeImmutable(),
            $message->limit,
        );

        if ($publishedCount > 0) {
            $this->logger?->info('Versions metier planifiees publiees par le scheduler.', [
                'published_count' => $publishedCount,
                'limit' => $message->limit,
            ]);
        }
    }
}
