<?php

namespace App\Service\Mailer;

use App\Service\Mailer\Event\CreatedAdminEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

#[AsEventListener(event: CreatedAdminEvent::class, method: 'onAdminEvent')]
class MailerSubscriber
{
    public function __construct(
        private readonly UrlGeneratorInterface $urlGenerator,
        private readonly Mailer $mailer
    ) {
    }

    public function onAdminEvent(CreatedAdminEvent $createdAdminEvent): void
    {
        $account = $createdAdminEvent->getAccount();
        $password = $createdAdminEvent->getPassword();

        $this->mailer->send(
            $account->getEmail(),
            $account->getUsername(),
            "[LudikEvent] - Création d'un compte administrateur",
            "mails/new_admin.twig",
            [
                'username' => $account->getUsername(),
                'password' => $password,
                'url' => $this->urlGenerator->generate('app_home'),
            ]
        );
    }
}
