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

use Ferienpass\CmsBundle\Message\LoginLinkHelpRequested;
use Ferienpass\CoreBundle\Notification\LoginLinkHelpNotification;
use Ferienpass\CoreBundle\Notifier\Notifier;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\Translation\TranslatorInterface;

#[AsMessageHandler(bus: 'event.bus')]
class LoginLinkHelp
{
    public function __construct(
        private readonly Notifier $notifier,
        private readonly LoginLinkHelpNotification $notification,
        private readonly TranslatorInterface $translator,
    ) {
    }

    public function __invoke(LoginLinkHelpRequested $event, Uuid $uuid): void
    {
        if ('' === $email = $event->getEmail()) {
            return;
        }

        $this->notifier->send(
            (clone $this->notification)
                ->emailAddress($email)
                ->subject($this->translator->trans('loginLinkHelpSubject', [], 'email'))
                ->belongsTo($uuid),
            new Recipient($email)
        );
    }
}
