<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Tests\MessageHandler;

use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Entity\Edition;
use Ferienpass\CoreBundle\Entity\Offer\Base;
use Ferienpass\CoreBundle\Entity\Participant\BaseParticipant;
use Ferienpass\CoreBundle\Message\WithdrawAttendance as WithdrawAttendanceCommand;
use Ferienpass\CoreBundle\MessageHandler\WithdrawAttendance;
use Ferienpass\CoreBundle\Repository\AttendanceRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Uid\Uuid;

final class WithdrawAttendanceTest extends TestCase
{
    public function testDoesNothingWhenAttendanceNotFound(): void
    {
        $attendanceUuid = Uuid::v7();
        $repo = $this->createMock(AttendanceRepository::class);
        $repo->method('findOneBy')->with(['uuid' => $attendanceUuid])->willReturn(null);

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(self::never())->method('dispatch');

        $handler = new WithdrawAttendance($repo, $bus);
        $handler(new WithdrawAttendanceCommand((string) $attendanceUuid, false, false), Uuid::v7());
    }

    public function testDispatchesReorderWhenShallReorderIsTrue(): void
    {
        $attendanceUuid = Uuid::v7();
        [$attendance, $repo] = $this->makeAttendanceWithRepo($attendanceUuid);

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(self::exactly(2))->method('dispatch')->willReturn(new Envelope(new \stdClass()));

        $handler = new WithdrawAttendance($repo, $bus);
        $handler(new WithdrawAttendanceCommand((string) $attendanceUuid, true, false), Uuid::v7());
    }

    public function testSkipsReorderWhenShallReorderIsFalse(): void
    {
        $attendanceUuid = Uuid::v7();
        [$attendance, $repo] = $this->makeAttendanceWithRepo($attendanceUuid);

        $dispatchedMessages = [];
        $bus = $this->createMock(MessageBusInterface::class);
        $bus->method('dispatch')->willReturnCallback(static function (object $msg) use (&$dispatchedMessages) {
            $dispatchedMessages[] = $msg;

            return new Envelope(new \stdClass());
        });

        $handler = new WithdrawAttendance($repo, $bus);
        $handler(new WithdrawAttendanceCommand((string) $attendanceUuid, false, false), Uuid::v7());

        self::assertCount(1, $dispatchedMessages);
    }

    private function makeAttendanceWithRepo(Uuid $attendanceUuid): array
    {
        $edition = new Edition();
        $editionRp = new \ReflectionProperty($edition, 'id');
        $editionRp->setValue($edition, 10);

        $offer = new Base();
        $offer->setEdition($edition);
        $offerRp = new \ReflectionProperty($offer, 'id');
        $offerRp->setValue($offer, 5);

        $participant = new BaseParticipant();
        $participantRp = new \ReflectionProperty($participant, 'id');
        $participantRp->setValue($participant, 3);

        $attendance = new Attendance($offer, $participant);

        $repo = $this->createMock(AttendanceRepository::class);
        $repo->method('findOneBy')->with(['uuid' => $attendanceUuid])->willReturn($attendance);

        return [$attendance, $repo];
    }
}
