<?php

declare(strict_types=1);

namespace app\vendor\apexl\io\src\Io\modules\system\commands;

use apexl\Io\factories\MailerFactory;
use PHPMailer\PHPMailer\Exception;
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;

#[AsCommand(name: 'debug:send-email', description: 'Send a test email')]
class SendEmailCommand extends DaedalusCommand
{
    public function __construct(
        private readonly MailerFactory $mailerFactory,
        ?string $name = null
    ) {
        parent::__construct($name);
    }

    protected function configure(): void
    {
        $this->addArgument('recipient', InputArgument::REQUIRED, 'The email to send to');
        $this->addOption('subject', 's', InputOption::VALUE_OPTIONAL, default: 'Test Email');
        $this->addOption('content', 'c', InputOption::VALUE_OPTIONAL, default: 'This is a test email');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $mailer = $this->mailerFactory->make();

        $mailer->Subject = $input->getOption('subject');
        $mailer->Body = $input->getOption('content');
        try {
            $mailer->addAddress($input->getArgument('recipient'));
            $mailer->send();
        } catch (Exception $e) {
            $io->error($e->getMessage());

            return Command::FAILURE;
        }

        $io->success(sprintf('Sent test email to %s', $input->getArgument('recipient')));

        return Command::SUCCESS;
    }
}
