<?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\ApplicationSystem\ApplicationSystems;
use Ferienpass\CoreBundle\ApplicationSystem\FirstComeApplicationSystem;
use Ferienpass\CoreBundle\Entity\Edition;
use Ferienpass\CoreBundle\Message\UpdateParticipantPriorities as UpdatePrioritiesCommand;
use Ferienpass\CoreBundle\MessageHandler\UpdateParticipantPriorities;
use Ferienpass\CoreBundle\Repository\EditionRepository;
use Ferienpass\CoreBundle\Repository\ParticipantRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Uuid;

final class UpdateParticipantPrioritiesTest extends TestCase
{
    public function testDoesNothingWhenParticipantNotFound(): void
    {
        $participantUuid = Uuid::v7();
        $participants = $this->createMock(ParticipantRepositoryInterface::class);
        $participants->method('findOneBy')->with(['uuid' => $participantUuid])->willReturn(null);

        $editions = $this->createMock(EditionRepository::class);
        $editions->expects(self::never())->method('findOneBy');

        $appSystems = $this->createMock(ApplicationSystems::class);
        $appSystems->expects(self::never())->method('findApplicationSystem');

        $handler = new UpdateParticipantPriorities($participants, $editions, $appSystems);
        $handler(new UpdatePrioritiesCommand((string) $participantUuid, (string) Uuid::v7()), Uuid::v7());
    }

    public function testDoesNothingWhenEditionNotFound(): void
    {
        $participant = new \Ferienpass\CoreBundle\Entity\Participant\BaseParticipant();
        $participantUuid = Uuid::v7();
        $editionUuid = Uuid::v7();

        $participants = $this->createMock(ParticipantRepositoryInterface::class);
        $participants->method('findOneBy')->with(['uuid' => $participantUuid])->willReturn($participant);

        $editions = $this->createMock(EditionRepository::class);
        $editions->method('findOneBy')->with(['uuid' => $editionUuid])->willReturn(null);

        $appSystems = $this->createMock(ApplicationSystems::class);
        $appSystems->expects(self::never())->method('findApplicationSystem');

        $handler = new UpdateParticipantPriorities($participants, $editions, $appSystems);
        $handler(new UpdatePrioritiesCommand((string) $participantUuid, (string) $editionUuid), Uuid::v7());
    }

    public function testDoesNothingWhenApplicationSystemIsNotLot(): void
    {
        $participant = new \Ferienpass\CoreBundle\Entity\Participant\BaseParticipant();
        $edition = new Edition();
        $participantUuid = Uuid::v7();
        $editionUuid = Uuid::v7();

        $participants = $this->createMock(ParticipantRepositoryInterface::class);
        $participants->method('findOneBy')->with(['uuid' => $participantUuid])->willReturn($participant);

        $editions = $this->createMock(EditionRepository::class);
        $editions->method('findOneBy')->with(['uuid' => $editionUuid])->willReturn($edition);

        // A FirstComeApplicationSystem is not a LotApplicationSystem → early return
        $appSystem = $this->createMock(FirstComeApplicationSystem::class);

        $appSystems = $this->createMock(ApplicationSystems::class);
        $appSystems->method('findApplicationSystem')->willReturn($appSystem);

        $handler = new UpdateParticipantPriorities($participants, $editions, $appSystems);
        // If we reach past the early return, it would call $appSystem->getTask() which would fail
        // So this test verifies the early return is triggered
        $handler(new UpdatePrioritiesCommand((string) $participantUuid, (string) $editionUuid), Uuid::v7());

        $this->addToAssertionCount(1);
    }
}
