<?php

namespace AutoAdmin\Bundle\Admin\Mailer;

use AutoAdmin\Bundle\Admin\Model\AdministratorInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

class AdminMailer
{
    private MailerInterface $mailer;
    private Environment $twig;

    public function __construct(MailerInterface $mailer, Environment $twig)
    {
        $this->mailer = $mailer;
        $this->twig = $twig;
    }

    /**
     * @throws LoaderError
     * @throws RuntimeError
     * @throws SyntaxError
     */
    public function onForgetPassword(AdministratorInterface $admin): void
    {
        $message = (new Email())
            // TODO Injecter le nom du site ET l'email depuis l'env ou la config du projet
            ->subject('Autoadmin : Mot de passe oublié ?')
            ->from('contact@gda.fr')
            ->to($admin->getEmail())
            ->html(
                $this->twig->render('@AutoAdminAdmin/email/resetting/reset.html.twig', [
                    'user' => $admin
                ]),
                'text/html'
            )
            ->text(
                $this->twig->render('@AutoAdminAdmin/email/resetting/reset.txt.twig', [
                    'user' => $admin
                ]),
                'text/plain'
            );

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