<?php

namespace App\Service;
use App\Entity\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;

class MailService
 {
    private MailerInterface $mailer;
    private EntityManagerInterface $entityManager;

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

    public function sendContactMail(array $data): void
    {
        $configuration = $this->entityManager->getRepository(Configuration::class)->findOneBy(['id' => 1]);
        $email = (new TemplatedEmail())
            ->from($configuration->getOwnerMail())
            ->to($configuration->getOwnerMail())
            ->replyTo($data["email"])
            ->subject('Nouvelle demande de contact')
            ->htmlTemplate('mail/contact.html.twig')
            ->context([
                'data' => $data,
            ]);

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