<?php
/**
 * SendTestMail Artisan Command
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @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 Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;


/**
 * SendTestMail Artisan Command
 *
 * @version     1.0.0 / 2026-04-30
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class SendTestMail extends Command
{


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


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


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


    /**
     * @var string
     */
    protected $description = 'Sends a test e-mail to a given address using the current .env mail configuration';


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


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


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


    /**
     * Execute the console command.
     *
     * @version     1.0.0 / 2026-04-30
     * @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 to ['.$email.']');
        $this->info('Mail driver:  '.config('mail.default'));
        $this->info('Mail host  :  '.config('mail.mailers.'.config('mail.default').'.host', '[none]'));
        $this->info('Mail from  :  '.config('mail.from.address'));
        $this->newLine();

        try {
            Mail::raw(
                'This is a test e-mail sent via the ['.$this->signature.'] Artisan command.'
                    ."\n\n".'Mail driver: '.config('mail.default')
                    ."\n".'Sent at    : '.now()->toDateTimeString(),

                function ($message) use ($email): void {
                    $message->to($email)->subject('Test Mail – '.config('app.name'));
                }
            );
        } 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 SendTestMail extends Command {}
