<?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\CmsBundle\Tests\Application\ExclusionReasons;

use Doctrine\Common\Collections\ArrayCollection;
use Ferienpass\CmsBundle\Application\ExclusionReasonEvent;
use Ferienpass\CmsBundle\Application\ExclusionReasons\LimitReached;
use Ferienpass\CmsBundle\Application\ExclusionReasons\LimitReachedListener;
use Ferienpass\CoreBundle\ApplicationSystem\ApplicationSystemInterface;
use Ferienpass\CoreBundle\ApplicationSystem\FirstComeApplicationSystem;
use Ferienpass\CoreBundle\Entity\Attendance;
use Ferienpass\CoreBundle\Entity\EditionTask;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\Participant\ParticipantInterface;
use PHPUnit\Framework\TestCase;

final class LimitReachedListenerTest extends TestCase
{
    public function testDoesNothingWhenNoTask(): void
    {
        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn(null);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $this->createMock(ParticipantInterface::class),
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testDoesNothingWhenNoMaxApplications(): void
    {
        $task = $this->createMock(EditionTask::class);
        $task->method('getMaxApplications')->willReturn(null);
        $task->method('isSkipMaxApplications')->willReturn(false);

        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn($task);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $this->createMock(ParticipantInterface::class),
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testDoesNothingWhenSkipMaxApplications(): void
    {
        $task = $this->createMock(EditionTask::class);
        $task->method('getMaxApplications')->willReturn(3);
        $task->method('isSkipMaxApplications')->willReturn(true);

        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn($task);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $this->createMock(ParticipantInterface::class),
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testDoesNothingWhenBelowLimit(): void
    {
        $task = $this->createMock(EditionTask::class);
        $task->method('getMaxApplications')->willReturn(3);
        $task->method('isSkipMaxApplications')->willReturn(false);
        $task->method('getId')->willReturn(1);

        $participant = $this->createMock(ParticipantInterface::class);
        $participant->method('getAttendancesNotHidden')->willReturn(new ArrayCollection());
        $participant->method('getFirstname')->willReturn('Max');

        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn($task);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $participant,
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testThrowsWhenLimitReached(): void
    {
        $task = $this->createMock(EditionTask::class);
        $task->method('getMaxApplications')->willReturn(3);
        $task->method('isSkipMaxApplications')->willReturn(false);
        $task->method('getId')->willReturn(1);

        $attendance = $this->createMock(Attendance::class);
        $attendance->method('getTask')->willReturn($task);

        $participant = $this->createMock(ParticipantInterface::class);
        $participant->method('getAttendancesNotHidden')->willReturn(new ArrayCollection([$attendance, $attendance, $attendance]));
        $participant->method('getFirstname')->willReturn('Max');

        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn($task);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $participant,
            $appSystem
        );

        $listener = new LimitReachedListener();

        $this->expectException(LimitReached::class);
        $listener($event);
    }

    public function testDoesNothingForFirstComeEvenWhenLimitReached(): void
    {
        // The firstcome task editor never exposes the per-phase total, so a set
        // maxApplications (e.g. a stale default carried over from a lot phase)
        // must be ignored — same setup as testThrowsWhenLimitReached, but the
        // firstcome system means no exception is thrown.
        $task = $this->createMock(EditionTask::class);
        $task->method('getMaxApplications')->willReturn(3);
        $task->method('isSkipMaxApplications')->willReturn(false);
        $task->method('getId')->willReturn(1);

        $attendance = $this->createMock(Attendance::class);
        $attendance->method('getTask')->willReturn($task);

        $participant = $this->createMock(ParticipantInterface::class);
        $participant->method('getAttendancesNotHidden')->willReturn(new ArrayCollection([$attendance, $attendance, $attendance]));
        $participant->method('getFirstname')->willReturn('Max');

        $appSystem = $this->createMock(FirstComeApplicationSystem::class);
        $appSystem->method('getTask')->willReturn($task);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $participant,
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testShortCircuitsForRealFirstComeApplicationSystem(): void
    {
        // Exercise the guard against the real FirstComeApplicationSystem (not a
        // mock), and assert the listener bails out before it ever counts
        // attendances — even for a task that has a maxApplications set.
        $task = $this->createMock(EditionTask::class);
        $task->method('isAnApplicationSystem')->willReturn(true);
        $task->method('getApplicationSystem')->willReturn(ApplicationSystemInterface::FIRSTCOME);
        $task->method('getMaxApplications')->willReturn(3);

        $appSystem = new FirstComeApplicationSystem()->withTask($task);

        $participant = $this->createMock(ParticipantInterface::class);
        $participant->expects($this->never())->method('getAttendancesNotHidden');

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $participant,
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);
    }
}
