<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Scheduler;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Scheduler\PurgeExpiredRegistrationsMessage;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Scheduler\PurgeExpiredRegistrationsMessageHandler;
use CpCreation\VitiCore\User\ExpiredRegistrationPurger;
use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

final class PurgeExpiredRegistrationsMessageHandlerTest extends TestCase
{
    public function testItForwardsTheBatchLimitsAndLogsOnlyAggregateCounts(): void
    {
        $connection = $this->createMock(Connection::class);
        $connection
            ->expects(self::once())
            ->method('transactional')
            ->willReturnCallback(static fn (callable $callback): mixed => $callback($connection))
        ;
        $connection
            ->expects(self::once())
            ->method('fetchAllAssociative')
            ->willReturnCallback(static function (string $sql, array $parameters): array {
                self::assertSame(25, $parameters['batchSize']);

                return [];
            })
        ;
        $logger = $this->createMock(LoggerInterface::class);
        $logger
            ->expects(self::once())
            ->method('info')
            ->with(
                'Purge des inscriptions expirees terminee.',
                self::callback(static function (array $context): bool {
                    self::assertSame([
                        'eligible_count',
                        'deleted_count',
                        'skipped_count',
                        'email_logs_deleted',
                        'audit_rows_deleted',
                        'batch_count',
                        'has_more',
                    ], array_keys($context));
                    self::assertNotContains('email', array_keys($context));
                    self::assertNotContains('user_id', array_keys($context));

                    return true;
                }),
            )
        ;

        $handler = new PurgeExpiredRegistrationsMessageHandler(
            new ExpiredRegistrationPurger($connection),
            $logger,
        );

        $handler(new PurgeExpiredRegistrationsMessage(batchSize: 25, maxBatches: 3));
    }
}
