<?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\Offer\Base;
use Ferienpass\CoreBundle\Entity\Participant\BaseParticipant;
use Ferienpass\CoreBundle\Message\RelaunchOffer as RelaunchOfferCommand;
use Ferienpass\CoreBundle\MessageHandler\RelaunchOffer;
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 RelaunchOfferTest extends TestCase
{
    public function testDoesNotDispatchWhenNoConfirmedAttendances(): void
    {
        $offerId = Uuid::v7();
        $repo = $this->createMock(AttendanceRepository::class);
        $repo->method('findConfirmed')->with($offerId)->willReturn([]);

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

        $handler = new RelaunchOffer($repo, $bus);
        $handler(new RelaunchOfferCommand((string) $offerId), Uuid::v7());
    }

    public function testDispatchesOneEventPerConfirmedAttendance(): void
    {
        $offer = new Base();
        $offerId = Uuid::v7();
        $participant = new BaseParticipant();
        $attendance1 = new Attendance($offer, $participant);
        $attendance2 = new Attendance($offer, $participant);

        $repo = $this->createMock(AttendanceRepository::class);
        $repo->method('findConfirmed')->with($offerId)->willReturn([$attendance1, $attendance2]);

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

        $handler = new RelaunchOffer($repo, $bus);
        $handler(new RelaunchOfferCommand((string) $offerId, 'Relaunch text'), Uuid::v7());
    }

    public function testRestoresPayableForPayableOffer(): void
    {
        $offer = new Base();
        $offer->setFee(1000);
        $offerId = Uuid::v7();
        $participant = new BaseParticipant();
        $attendance = new Attendance($offer, $participant);
        // Emulate the state left behind by CancelOffer.
        $attendance->setPayable(false);

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

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

        $handler = new RelaunchOffer($repo, $bus);
        $handler(new RelaunchOfferCommand((string) $offerId), Uuid::v7());

        self::assertTrue($attendance->isPayable());
    }

    public function testDoesNotRestorePayableForNonPayableOffer(): void
    {
        // No fee → attendance was never payable, so relaunch must leave it alone.
        $offer = new Base();
        $offerId = Uuid::v7();
        $participant = new BaseParticipant();
        $attendance = new Attendance($offer, $participant);
        $attendance->setPayable(false);

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

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

        $handler = new RelaunchOffer($repo, $bus);
        $handler(new RelaunchOfferCommand((string) $offerId), Uuid::v7());

        self::assertFalse($attendance->isPayable());
    }
}
