<?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\NotificationHandler;

use Ferienpass\CoreBundle\Message\OfferCancelled as OfferCancelledEvent;
use Ferienpass\CoreBundle\Message\OfferFinalized as OfferFinalizedEvent;
use Ferienpass\CoreBundle\NotificationHandler\OfferCancelled;
use Ferienpass\CoreBundle\NotificationHandler\OfferFinalized;
use Ferienpass\CoreBundle\NotificationHandler\PhoneFormatter;
use Ferienpass\CoreBundle\Notifier\Notifier;
use Ferienpass\CoreBundle\Repository\AttendanceRepository;
use Ferienpass\CoreBundle\Repository\OfferRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Uid\Uuid;

final class OfferNotificationsTest extends TestCase
{
    // ---- OfferFinalized ----

    public function testOfferFinalizedDoesNothingWhenOfferNotFound(): void
    {
        $offerId = Uuid::v7();
        $offers = $this->createMock(OfferRepositoryInterface::class);
        $offers->method('findOneBy')->with(['uuid' => $offerId])->willReturn(null);

        $notifier = $this->createMock(Notifier::class);
        $notifier->expects(self::never())->method('send');

        $handler = new OfferFinalized($notifier, $offers, $this->createMock(UrlGeneratorInterface::class));
        $handler(new OfferFinalizedEvent((string) $offerId), Uuid::v7());
    }

    public function testOfferFinalizedDoesNothingWhenNotifierReturnsNull(): void
    {
        $offer = new \Ferienpass\CoreBundle\Entity\Offer\Base();
        $offerId = Uuid::v7();

        $offers = $this->createMock(OfferRepositoryInterface::class);
        $offers->method('findOneBy')->with(['uuid' => $offerId])->willReturn($offer);

        $notifier = $this->createMock(Notifier::class);
        $notifier->method('offerFinalized')->willReturn(null);
        $notifier->expects(self::never())->method('send');

        $handler = new OfferFinalized($notifier, $offers, $this->createMock(UrlGeneratorInterface::class));
        $handler(new OfferFinalizedEvent((string) $offerId), Uuid::v7());
    }

    // ---- OfferCancelled ----

    public function testOfferCancelledDoesNothingWhenNotifierReturnsNull(): void
    {
        $offer = new \Ferienpass\CoreBundle\Entity\Offer\Base();
        $participant = new \Ferienpass\CoreBundle\Entity\Participant\BaseParticipant();
        $attendance = new \Ferienpass\CoreBundle\Entity\Attendance($offer, $participant);
        $attendanceUuid = Uuid::v7();

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

        $notifier = $this->createMock(Notifier::class);
        $notifier->method('offerCancelled')->willReturn(null);
        $notifier->expects(self::never())->method('send');

        $handler = new OfferCancelled(
            $notifier,
            $attendances,
            new PhoneFormatter()
        );
        $handler(new OfferCancelledEvent((string) $attendanceUuid, (string) Uuid::v7()), Uuid::v7());
    }
}
