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

use Ferienpass\CmsBundle\Message\PasswordLinkCreated;
use Ferienpass\CmsBundle\NotificationHandler\PasswordLink;
use Ferienpass\CoreBundle\Entity\User;
use Ferienpass\CoreBundle\Notification\UserPasswordNotification;
use Ferienpass\CoreBundle\Notifier\Notifier;
use Ferienpass\CoreBundle\Repository\UserRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\Translation\TranslatorInterface;

final class PasswordLinkTest extends TestCase
{
    public function testFallsBackToDefaultContentWhenNotifierReturnsNull(): void
    {
        $user = new User();
        $user->setEmail('user@example.com');

        $users = $this->createMock(UserRepository::class);
        $users->method('findOneBy')->willReturn($user);

        // No enabled `user_password` notification row for this tenant. The mail
        // must still go out — returning early here silently swallowed every
        // password reset for that tenant.
        $notifier = $this->createMock(Notifier::class);
        $notifier->method('userPassword')->willReturn(null);
        $notifier->expects(self::once())->method('send')
            ->with(
                self::isInstanceOf(UserPasswordNotification::class),
                self::callback(static fn (Recipient $r): bool => 'user@example.com' === $r->getEmail()),
            );

        $handler = new PasswordLink(
            $notifier,
            $users,
            $this->createMock(UrlGeneratorInterface::class),
            new UserPasswordNotification(),
            $this->createMock(TranslatorInterface::class),
        );
        $handler(new PasswordLinkCreated(Uuid::v7()->toRfc4122(), 'my-token'), Uuid::v7());
    }

    public function testSendsConfiguredNotificationWhenOneExists(): void
    {
        $user = new User();
        $user->setEmail('user@example.com');

        $users = $this->createMock(UserRepository::class);
        $users->method('findOneBy')->willReturn($user);

        $configured = new UserPasswordNotification();

        $notifier = $this->createMock(Notifier::class);
        $notifier->method('userPassword')->willReturn($configured);
        $notifier->expects(self::once())->method('send')
            ->with(
                self::identicalTo($configured),
                self::callback(static fn (Recipient $r): bool => 'user@example.com' === $r->getEmail()),
            );

        $handler = new PasswordLink(
            $notifier,
            $users,
            $this->createMock(UrlGeneratorInterface::class),
            new UserPasswordNotification(),
            $this->createMock(TranslatorInterface::class),
        );
        $handler(new PasswordLinkCreated(Uuid::v7()->toRfc4122(), 'my-token'), Uuid::v7());
    }

    public function testDoesNothingWhenUserIsGone(): void
    {
        $users = $this->createMock(UserRepository::class);
        $users->method('findOneBy')->willReturn(null);

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

        $handler = new PasswordLink(
            $notifier,
            $users,
            $this->createMock(UrlGeneratorInterface::class),
            new UserPasswordNotification(),
            $this->createMock(TranslatorInterface::class),
        );
        $handler(new PasswordLinkCreated(Uuid::v7()->toRfc4122(), 'my-token'), Uuid::v7());
    }
}
