<?php

namespace App\Command;

use App\Service\MailerService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Twig\Environment;

#[AsCommand(
    name: 'app:mail:test',
    description: 'Envoie un email de test complet (tableau, texte, bouton, lien, fichier) pour verifier le rendu',
)]
class TestMailCommand extends Command
{
    public function __construct(
        private MailerService $mailer,
        private Environment $twig,
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addArgument('email', InputArgument::REQUIRED, 'Adresse email du destinataire')
            ->addOption('mode', null, InputOption::VALUE_REQUIRED, 'Mode d\'envoi (dev ou prod)', 'dev')
            ->addOption('force-dsn', null, InputOption::VALUE_REQUIRED, 'Force un DSN SMTP specifique (ex: ses+smtp://...@default?region=eu-west-3) pour envoyer directement sans passer par le transport local');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $email = $input->getArgument('email');
        $env = $input->getOption('mode');
        $forceDsn = $input->getOption('force-dsn');

        $subject = 'prod' === $env
            ? '[PROD] CRM E-Cosplay - Email de test production'
            : '[DEV] CRM E-Cosplay - Email de test developpement';

        $io->title('Envoi du mail de test ('.$env.')');
        $io->text('Destinataire : '.$email);

        $html = $this->twig->render('emails/test_mail.html.twig', [
            'env' => $env,
            'email' => $email,
            'date' => new \DateTimeImmutable(),
        ]);

        if (null !== $forceDsn) {
            return $this->sendViaForceDsn($forceDsn, $email, $subject, $html, $io);
        }

        $this->mailer->sendEmail(
            $email,
            $subject,
            $html,
            null,
            null,
            false,
        );

        $io->success('Email de test envoye a '.$email.' (env: '.$env.')');

        return Command::SUCCESS;
    }

    private function sendViaForceDsn(string $dsn, string $to, string $subject, string $html, SymfonyStyle $io): int
    {
        $io->text('Force DSN : '.$dsn);

        try {
            $transport = Transport::fromDsn($dsn);
            $directMailer = new Mailer($transport);

            $email = (new Email())
                ->from('Association E-Cosplay <client@e-cosplay.fr>')
                ->to($to)
                ->subject($subject)
                ->html($html);

            $directMailer->send($email);

            $io->success('Email envoye directement via force-dsn a '.$to);

            return Command::SUCCESS;
        } catch (\Throwable $e) {
            $io->error('Echec envoi via force-dsn : '.$e->getMessage());

            return Command::FAILURE;
        }
    }
}
