<?php

namespace App\Service\Mailer;

use App\Entity\Mail;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\IdentificationHeader;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class AmazonSesClient
{
    private Mailer $mailer;

    public function __construct(
        private UrlGeneratorInterface $urlGenerator,
        EventDispatcherInterface $eventDispatcher
    ) {
        $transport = new EsmtpTransport(
            'email-smtp.eu-west-3.amazonaws.com',
            587,
            false,
            $eventDispatcher
        );
        $transport->setUsername($_ENV['AMAZON_SES_PUBLIC'] ?? '');
        $transport->setPassword($_ENV['AMAZON_SES_SECRET'] ?? '');

        $this->mailer = new Mailer($transport);
    }

    /**
     * Envoie un email HTML avec tracking invisible.
     */
    public function send(string $html, string $address, string $subject): void
    {
        $email = (new Email())
            ->from('no-reply@siteconseil.fr')
            ->to($address)
            ->subject($subject);

        // Génération et ajout de l'en-tête Message-Id
        $messageId = $email->generateMessageId();
        $email->getHeaders()->add(new IdentificationHeader('Message-Id', $messageId));

        $tracking = $this->generateTracking($email);

        $dom = new \DOMDocument();
        libxml_use_internal_errors(true);
        $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        libxml_clear_errors();

        $img = $dom->createElement('img');
        $img->setAttribute('src', $tracking['url']);
        $img->setAttribute('width', '0');
        $img->setAttribute('height', '0');

        $body = $dom->getElementsByTagName('body')->item(0);
        if ($body) {
            $body->appendChild($img);
        }
        $newHtml = $dom->saveHTML();

        $email->html($newHtml);

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

    /**
     * Génère les données de tracking et l’URL associée.
     *
     * @param Email $email
     * @return array{object: Mail, url: string}
     */
    private function generateTracking(Email $email): array
    {
        $messageIdHeader = $email->getHeaders()->get('message-id');
        $messageFormat = $messageIdHeader ? $messageIdHeader->getBody()[0] : '';
        $messageFormat = str_replace('@siteconseil.fr', '', $messageFormat);

        $mailData = new Mail();
        $mailData->setDest($email->getTo()[0]->getAddress());
        $mailData->setSubject($email->getSubject());
        $mailData->setMessageId($messageFormat);
        $mailData->setStatus('draft');

        return [
            'object' => $mailData,
            'url' => 'https://mainframe.esy-web.dev' . $this->urlGenerator->generate('app_tracking', ['slug' => $messageFormat]),
        ];
    }
}
