<?php

namespace App\Tests\Command;

use App\Command\ExpirePendingOrdersCommand;
use App\Entity\Billet;
use App\Entity\BilletBuyer;
use App\Entity\BilletBuyerItem;
use App\Entity\BilletOrder;
use App\Entity\Event;
use App\Repository\BilletBuyerRepository;
use App\Service\AuditService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class ExpirePendingOrdersCommandTest extends TestCase
{
    private EntityManagerInterface $em;
    private BilletBuyerRepository $buyerRepo;
    private AuditService $audit;
    private CommandTester $tester;

    protected function setUp(): void
    {
        $this->em = $this->createMock(EntityManagerInterface::class);
        $this->buyerRepo = $this->createMock(BilletBuyerRepository::class);
        $this->audit = $this->createMock(AuditService::class);

        $command = new ExpirePendingOrdersCommand($this->em, $this->buyerRepo, $this->audit);
        $app = new Application();
        $app->addCommand($command);
        $this->tester = new CommandTester($app->find('app:orders:expire-pending'));
    }

    public function testNoExpiredOrders(): void
    {
        $this->buyerRepo->method('findExpiredPending')->willReturn([]);
        $this->em->expects($this->never())->method('flush');

        $this->tester->execute([]);
        $this->assertStringContainsString('No expired pending orders', $this->tester->getDisplay());
    }

    public function testExpiresOldPendingOrders(): void
    {
        $event = $this->createMock(Event::class);
        $event->method('getTitle')->willReturn('Test Event');

        $billet = $this->createMock(Billet::class);
        $billet->method('getQuantity')->willReturn(5);

        $item = $this->createMock(BilletBuyerItem::class);
        $item->method('getBillet')->willReturn($billet);
        $item->method('getQuantity')->willReturn(2);

        $order = $this->createMock(BilletBuyer::class);
        $order->method('getId')->willReturn(1);
        $order->method('getOrderNumber')->willReturn('2026-03-23-1');
        $order->method('getEvent')->willReturn($event);
        $order->method('getItems')->willReturn(new ArrayCollection([$item]));

        $order->expects($this->once())->method('setStatus')->with('cancelled');
        $billet->expects($this->once())->method('setQuantity')->with(7);

        $this->buyerRepo->method('findExpiredPending')->willReturn([$order]);

        $ticket = $this->createMock(BilletOrder::class);
        $ticket->expects($this->once())->method('setState')->with(BilletOrder::STATE_INVALID);

        $ticketRepo = $this->createMock(EntityRepository::class);
        $ticketRepo->method('findBy')->willReturn([$ticket]);

        $this->em->method('getRepository')
            ->with(BilletOrder::class)
            ->willReturn($ticketRepo);

        $this->em->expects($this->once())->method('flush');

        $this->audit->expects($this->once())
            ->method('log')
            ->with('order_expired', 'BilletBuyer', 1, $this->anything());

        $this->tester->execute([]);
        $this->assertStringContainsString('1 pending order(s) expired', $this->tester->getDisplay());
    }

    public function testExpiresOrderWithUnlimitedBillet(): void
    {
        $event = $this->createMock(Event::class);
        $event->method('getTitle')->willReturn('Test');

        $billet = $this->createMock(Billet::class);
        $billet->method('getQuantity')->willReturn(null);

        $item = $this->createMock(BilletBuyerItem::class);
        $item->method('getBillet')->willReturn($billet);
        $item->method('getQuantity')->willReturn(1);

        $order = $this->createMock(BilletBuyer::class);
        $order->method('getId')->willReturn(2);
        $order->method('getOrderNumber')->willReturn('2026-03-23-2');
        $order->method('getEvent')->willReturn($event);
        $order->method('getItems')->willReturn(new ArrayCollection([$item]));

        $billet->expects($this->never())->method('setQuantity');

        $this->buyerRepo->method('findExpiredPending')->willReturn([$order]);

        $ticketRepo = $this->createMock(EntityRepository::class);
        $ticketRepo->method('findBy')->willReturn([]);

        $this->em->method('getRepository')
            ->with(BilletOrder::class)
            ->willReturn($ticketRepo);

        $this->tester->execute([]);
        $this->assertStringContainsString('1 pending order(s) expired', $this->tester->getDisplay());
    }
}
