<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Mail\TestMail;

class SendTestMail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mail:send-test {email?} {--body=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send a test email to the given address or MAIL_USERNAME';

    public function handle(): int
    {
        $email = $this->argument('email') ?? env('MAIL_USERNAME') ?? 'gregor@klevze.com';
        $body = $this->option('body') ?? "This is a test email sent by php artisan mail:send-test.";

        try {
            Mail::to($email)->send(new TestMail($body));
        } catch (\Exception $e) {
            $this->error('Failed to send mail: ' . $e->getMessage());
            return 1;
        }

        $this->info("Test mail sent to {$email}");
        return 0;
    }
}
