<?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\Entity\Edition;
use Ferienpass\CoreBundle\Entity\Offer\Base;
use Ferienpass\CoreBundle\Message\DecisionLetterDispatched as DecisionLetterDispatchedEvent;
use Ferienpass\CoreBundle\Message\OfferCompleted as OfferCompletedEvent;
use Ferienpass\CoreBundle\NotificationHandler\DecisionLetterDispatched;
use Ferienpass\CoreBundle\NotificationHandler\OfferCompleted;
use Ferienpass\CoreBundle\Notifier\Notifier;
use Ferienpass\CoreBundle\Repository\AttendanceRepository;
use Ferienpass\CoreBundle\Repository\EditionRepository;
use Ferienpass\CoreBundle\Repository\OfferRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\Translation\TranslatorInterface;

final class OtherNotificationsTest extends TestCase
{
    // ---- DecisionLetterDispatched ----

    public function testDecisionLetterDispatchedDoesNothingWhenNotifierReturnsNull(): void
    {
        $attendances = $this->createMock(AttendanceRepository::class);
        $attendances->method('findBy')->willReturn([]);

        $editions = $this->createMock(EditionRepository::class);
        $editions->method('find')->willReturn(new Edition());

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

        $handler = new DecisionLetterDispatched($notifier, $attendances, $editions);
        $handler(
            new DecisionLetterDispatchedEvent('test@example.com', (string) Uuid::v7(), [(string) Uuid::v7(), (string) Uuid::v7()], (string) Uuid::v7()),
            Uuid::v7()
        );
    }

    // ---- OfferCompleted ----

    public function testOfferCompletedDoesNothingWhenOfferNotFound(): void
    {
        $offers = $this->createMock(OfferRepositoryInterface::class);
        $offers->method('find')->willReturn(null);

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

        $handler = new OfferCompleted(
            $notifier,
            $offers,
            $this->createMock(UrlGeneratorInterface::class),
            $this->createMock(TranslatorInterface::class)
        );
        $handler(new OfferCompletedEvent((string) Uuid::v7(), []), Uuid::v7());
    }

    public function testOfferCompletedDoesNothingWhenNotifierReturnsNull(): void
    {
        $offer = new Base();

        $offers = $this->createMock(OfferRepositoryInterface::class);
        $offers->method('find')->willReturn($offer);

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

        $handler = new OfferCompleted(
            $notifier,
            $offers,
            $this->createMock(UrlGeneratorInterface::class),
            $this->createMock(TranslatorInterface::class)
        );
        $handler(new OfferCompletedEvent((string) Uuid::v7(), []), Uuid::v7());
    }

    // ---- AccountCreated ----

    public function testAccountCreatedDoesNothingWhenUserNotFound(): void
    {
        $users = $this->createMock(\Ferienpass\CoreBundle\Repository\UserRepository::class);
        $users->method('find')->willReturn(null);

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

        $handler = new \Ferienpass\CoreBundle\NotificationHandler\AccountCreated(
            $notifier,
            $users,
            $this->createMock(UrlGeneratorInterface::class),
            $this->createMock(\Symfony\Component\HttpFoundation\UriSigner::class),
            new \Ferienpass\CoreBundle\NotificationHandler\PhoneFormatter()
        );
        $handler(new \Ferienpass\CoreBundle\Message\AccountCreated((string) Uuid::v7()), Uuid::v7());
    }

    public function testAccountCreatedDoesNothingWhenNotifierReturnsNull(): void
    {
        $user = new \Ferienpass\CoreBundle\Entity\User();
        $user->setEmail('test@example.com');

        $users = $this->createMock(\Ferienpass\CoreBundle\Repository\UserRepository::class);
        $users->method('find')->willReturn($user);

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

        $handler = new \Ferienpass\CoreBundle\NotificationHandler\AccountCreated(
            $notifier,
            $users,
            $this->createMock(UrlGeneratorInterface::class),
            $this->createMock(\Symfony\Component\HttpFoundation\UriSigner::class),
            new \Ferienpass\CoreBundle\NotificationHandler\PhoneFormatter()
        );
        $handler(new \Ferienpass\CoreBundle\Message\AccountCreated((string) Uuid::v7()), Uuid::v7());
    }
}
