<?php

namespace App\Domains\MailTest\Services;

use App\Domains\Integration\Services\MailService;
use App\Domains\Tenant\Models\Tenant;

class MailTestService
{
    /**
     * @return array<string, string>
     */
    public function send(Tenant $tenant, string $recipient, ?string $subject = null, ?string $message = null): array
    {
        $subject ??= 'Prueba de correo de Shopit';
        $message ??= 'Este es un correo de prueba enviado desde Shopit.';

        $mailService = (new MailService)->forTenant($tenant->codigo);
        $mailService->send(
            $recipient,
            $subject,
            '<h1 style="margin: 0 0 20px;">'.e($subject).'</h1>'
                .'<p>'.nl2br(e($message)).'</p>',
        );

        return [
            'code' => 'mail.test_sent',
            'message' => __('api.mail.test_sent'),
            'recipient' => $recipient,
            'tenant_code' => $tenant->codigo,
            'mailer' => $mailService->mailerName(),
            'sent_at' => now()->toIso8601String(),
        ];
    }
}
