<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\TicketBundle\tests\Messenger\Handler;

use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Event\PostTicketUpdateEvent;
use Chill\TicketBundle\Event\TicketUpdateKindEnum;
use Chill\TicketBundle\Messenger\PostTicketUpdateMessage;
use Chill\TicketBundle\Messenger\Handler\PostTicketUpdateMessageHandler;
use Chill\TicketBundle\Repository\TicketRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
 * @covers \Chill\TicketBundle\Messenger\Handler\PostTicketUpdateMessageHandler
 *
 * @internal
 */
class PostTicketUpdateMessageHandlerTest extends TestCase
{
    use ProphecyTrait;

    public function testDispatchesEventWhenTicketExists(): void
    {
        // Arrange: a Ticket with an ID
        $ticket = new Ticket();
        $reflection = new \ReflectionClass(Ticket::class);
        $idProperty = $reflection->getProperty('id');
        $idProperty->setValue($ticket, 123);

        $message = new PostTicketUpdateMessage($ticket, TicketUpdateKindEnum::UPDATE_MOTIVE);

        // Mock repository to return the Ticket when searching by id
        $ticketRepository = $this->prophesize(TicketRepositoryInterface::class);
        $ticketRepository->find(123)->willReturn($ticket)->shouldBeCalledOnce();

        // Expect the dispatcher to dispatch a PostTicketUpdateEvent with correct data
        $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
        $eventDispatcher
            ->dispatch(Argument::that(fn ($event) => $event instanceof PostTicketUpdateEvent
                && TicketUpdateKindEnum::UPDATE_MOTIVE === $event->updateKind
                && $event->ticket === $ticket))
            ->will(fn ($args) => $args[0])
            ->shouldBeCalledOnce();

        $handler = new PostTicketUpdateMessageHandler($eventDispatcher->reveal(), $ticketRepository->reveal());

        // Act
        $handler($message);

        // Assert: expectations asserted by Prophecy
        self::assertTrue(true);
    }

    public function testThrowsWhenTicketNotFound(): void
    {
        // Arrange: a Ticket with an ID for the message, but repository will return null
        $ticket = new Ticket();
        $reflection = new \ReflectionClass(Ticket::class);
        $idProperty = $reflection->getProperty('id');
        $idProperty->setValue($ticket, 999);

        $message = new PostTicketUpdateMessage($ticket, TicketUpdateKindEnum::UPDATE_MOTIVE);

        $ticketRepository = $this->prophesize(TicketRepositoryInterface::class);
        $ticketRepository->find(999)->willReturn(null)->shouldBeCalledOnce();

        $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
        $eventDispatcher->dispatch(Argument::any())->shouldNotBeCalled();

        $handler = new PostTicketUpdateMessageHandler($eventDispatcher->reveal(), $ticketRepository->reveal());

        // Assert: exception is thrown
        $this->expectException(UnrecoverableMessageHandlingException::class);
        $this->expectExceptionMessage('Ticket not found');

        // Act
        $handler($message);
    }
}
