<?php

declare(strict_types=1);

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

use CpCreation\VitiCore\Marketing\ReviewInvitationDispatcher;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final readonly class DispatchReviewInvitationsMessageHandler
{
    public function __construct(
        private ReviewInvitationDispatcher $dispatcher,
        private ?LoggerInterface $logger = null,
    ) {}

    public function __invoke(DispatchReviewInvitationsMessage $message): void
    {
        $result = $this->dispatcher->dispatchDue(limit: $message->limit);

        if ($result->createdCount > 0 || $result->reminderQueuedCount > 0) {
            $this->logger?->info('Invitations et rappels d’avis mis en file par le scheduler.', [
                'eligible_count' => $result->eligibleCount,
                'created_count' => $result->createdCount,
                'skipped_count' => $result->skippedCount,
                'reminder_queued_count' => $result->reminderQueuedCount,
            ]);
        }
    }
}
