<?php

namespace App\Tests\Command;

use App\Command\PurgeEmailTrackingCommand;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

class PurgeEmailTrackingCommandTest extends TestCase
{
    private function createQueryBuilderStub(int $result): QueryBuilder
    {
        $query = $this->createStub(Query::class);
        $query->method('execute')->willReturn($result);

        $qb = $this->createStub(QueryBuilder::class);
        $qb->method('delete')->willReturnSelf();
        $qb->method('where')->willReturnSelf();
        $qb->method('setParameter')->willReturnSelf();
        $qb->method('getQuery')->willReturn($query);

        return $qb;
    }

    public function testExecuteDefault(): void
    {
        $em = $this->createStub(EntityManagerInterface::class);
        $em->method('createQueryBuilder')->willReturn($this->createQueryBuilderStub(5));

        $command = new PurgeEmailTrackingCommand($em);
        $tester = new CommandTester($command);
        $tester->execute([]);

        $this->assertSame(0, $tester->getStatusCode());
        $this->assertStringContainsString('10 enregistrement(s) supprime(s) au total', $tester->getDisplay());
    }

    public function testExecuteCustomDays(): void
    {
        $em = $this->createStub(EntityManagerInterface::class);
        $em->method('createQueryBuilder')->willReturn($this->createQueryBuilderStub(0));

        $command = new PurgeEmailTrackingCommand($em);
        $tester = new CommandTester($command);
        $tester->execute(['--days' => '30']);

        $this->assertSame(0, $tester->getStatusCode());
        $this->assertStringContainsString('Purge EmailTracking (> 30 jours)', $tester->getDisplay());
        $this->assertStringContainsString('0 enregistrement(s) supprime(s) au total', $tester->getDisplay());
    }
}
