<?php
/**
 * SendTestPhpMailer Artisan Command
 *
 * @version     1.0.0 / 2026-07-22
 * @package     bplan-base/globals
 * @subpackage  Commands
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Console\Commands;


use BplanBase\Globals\Mail\PHPMailer;
use Illuminate\Console\Command;


/**
 * SendTestPhpMailer Artisan Command
 *
 * @version     1.0.0 / 2026-07-22
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class SendTestPhpMailer extends Command
{


/* +++ TRAITS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * @var string
     */
    protected $description = 'Sends a test e-mail to a given address using the PHPMailer class from the Globals package';


    /**
     * @var string
     */
    protected $signature = 'globals:send-test-phpmailer {email? : The recipient e-mail address}';


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * Execute the console command.
     *
     * @version     1.0.0 / 2026-07-22
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function handle(): int
    {
        $email = $this->argument('email');

        if (empty($email) === true) {
            $email = $this->ask('Recipient e-mail address');
        }

        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
            $this->error('Invalid e-mail address ['.$email.']');

            return self::FAILURE;
        }
        $this->info('Sending test mail via PHPMailer to ['.$email.']');
        $this->info(' Mail driver:  '.config('mail.default'));
        $this->info(' SMTP host  :  '.config('mail.mailers.smtp.host', '[none]'));
        $this->info(' Mail from  :  '.config('mail.from.address'));
        $this->newLine();

        try {
            $Mailer = new PHPMailer(true);
            $Mailer->setDebugLevel(PHPMailer::DEBUG_SERVER);
            $Mailer->addAddress($email);
            $Mailer->isHTML(true);

            $Mailer->Subject = 'Test Mail (PHPMailer) – '.config('app.name');
            $Mailer->Body = 'This is a test e-mail sent via the ['.$this->signature.'] Artisan command using the PHPMailer class.'
                .'<br><br>Mail driver: '.config('mail.default')
                .'<br>Sent at    : '.now()->toDateTimeString();
            $Mailer->AltBody = strip_tags(str_replace('<br>', "\n", $Mailer->Body));

            $Mailer->send();
        } catch (\Throwable $E) {
            $this->error('Failed to send mail: '.$E->getMessage());

            return self::FAILURE;
        }
        $this->info('Test mail sent successfully.');

        return self::SUCCESS;

    } // handle()


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


} // class SendTestPhpMailer extends Command {}
