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

use Ferienpass\CoreBundle\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Message\EmailMessage as SymfonyEmailMessage;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Symfony\Component\Notifier\Recipient\RecipientInterface;

/**
 * Sent when a magic login link is requested for an e-mail address that has no
 * account. Tells the mailbox owner that no account is linked to the address so
 * they can retry with the right one (or ignore the mail). The content is fully
 * templated — it never relies on database-configured text — so the mail always
 * sends, like the login-link mail. The HTTP response is identical whether or
 * not an account exists, so this stays enumeration-safe.
 */
class LoginLinkHelpNotification extends AbstractNotification implements NotificationInterface, EmailNotificationInterface
{
    private string $emailAddress = '';

    public static function getName(): string
    {
        return 'login_link_help';
    }

    public function getChannels(RecipientInterface $recipient): array
    {
        return ['email'];
    }

    public function emailAddress(string $emailAddress): static
    {
        $this->emailAddress = $emailAddress;

        return $this;
    }

    public function getContext(): array
    {
        return array_merge(parent::getContext(), [
            'email_address' => $this->emailAddress,
        ]);
    }

    public static function getAvailableTokens(): array
    {
        return array_merge(parent::getAvailableTokens(), ['email_address']);
    }

    public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): ?SymfonyEmailMessage
    {
        return EmailMessage::fromFerienpassNotification($this, $recipient);
    }
}
