<?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\Bridge\Symfony\Bundle\Messenger\Messaging\CommandHandlerInterface;
use CpCreation\VitiCore\User\ExpiredRegistrationPurger;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final readonly class PurgeExpiredRegistrationsMessageHandler implements CommandHandlerInterface
{
    public function __construct(
        private ExpiredRegistrationPurger $purger,
        private ?LoggerInterface $logger = null,
    ) {
    }

    public function __invoke(PurgeExpiredRegistrationsMessage $message): void
    {
        $result = $this->purger->purge(
            batchSize: $message->batchSize,
            maxBatches: $message->maxBatches,
        );

        $this->logger?->info('Purge des inscriptions expirees terminee.', [
            'eligible_count' => $result->eligibleCount,
            'deleted_count' => $result->deletedCount,
            'skipped_count' => $result->skippedCount,
            'email_logs_deleted' => $result->emailLogsDeleted,
            'audit_rows_deleted' => $result->auditRowsDeleted,
            'batch_count' => $result->batchCount,
            'has_more' => $result->hasMore,
        ]);
    }
}
