<?php

namespace App\Security;

use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Twig\Environment;

class TwoFactorCodeMailer implements AuthCodeMailerInterface
{
    public function __construct(
        private MailerInterface $mailer,
        private Environment $twig,
    ) {
    }

    public function sendAuthCode(TwoFactorInterface $user): void
    {
        $code = $user->getEmailAuthCode();

        $html = $this->twig->render('emails/2fa_code.html.twig', [
            'code' => $code,
        ]);

        $email = (new Email())
            ->from('CRM E-Cosplay <client@e-cosplay.fr>')
            ->to($user->getEmailAuthRecipient())
            ->subject('CRM E-Cosplay - Code de verification')
            ->html($html);

        $this->mailer->send($email);
    }
}
