<?php

namespace App\Mail;

use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Facades\Log;

class MailpitCopyListener
{
    public function handle(MessageSending $event): void
    {
        if (!timebank_config('mailing.copy_to_mailpit', false)) {
            return;
        }

        try {
            $host = timebank_config('mailing.mailpit_host', 'localhost');
            $port = timebank_config('mailing.mailpit_port', 1025);

            $transport = new \Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport($host, $port, false);
            $transport->send($event->message);
        } catch (\Throwable $e) {
            // Never let mailpit copy failure break the real email
            Log::warning('Mailpit copy failed: ' . $e->getMessage());
        }
    }
}
