<?php

declare(strict_types=1);

namespace ImovelZapAi\Organizations\Infrastructure\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

final class OrganizationInvitationMail extends Mailable
{
    use Queueable;
    use SerializesModels;

    public function __construct(
        public readonly string $organizationName,
        public readonly string $inviteUrl,
        public readonly string $roleLabel,
        public readonly ?string $inviterName = null,
    ) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Convite para a equipe — '.$this->organizationName,
        );
    }

    public function content(): Content
    {
        return new Content(
            htmlString: $this->htmlBody(),
        );
    }

    private function htmlBody(): string
    {
        $who = $this->inviterName !== null && $this->inviterName !== ''
            ? e($this->inviterName)
            : 'Sua equipe';
        $org = e($this->organizationName);
        $role = e($this->roleLabel);
        $url = e($this->inviteUrl);

        return <<<HTML
<!DOCTYPE html>
<html lang="pt-BR">
<head><meta charset="utf-8"><title>Convite</title></head>
<body style="font-family: system-ui, sans-serif; line-height: 1.5; color: #0f172a;">
  <p>Olá,</p>
  <p><strong>{$who}</strong> convidou você para fazer parte de <strong>{$org}</strong> como <strong>{$role}</strong>.</p>
  <p>Clique no botão abaixo para criar sua senha e entrar:</p>
  <p style="margin: 24px 0;">
    <a href="{$url}" style="background:#059669;color:#fff;padding:12px 20px;border-radius:8px;text-decoration:none;font-weight:600;">
      Aceitar convite
    </a>
  </p>
  <p style="font-size:14px;color:#64748b;">Se o botão não funcionar, copie e cole este link no navegador:<br>{$url}</p>
  <p style="font-size:14px;color:#64748b;">Se você não esperava este convite, ignore este e-mail.</p>
  <p style="margin-top:24px;">Atenciosamente,<br>ImóvelZapAi</p>
</body>
</html>
HTML;
    }
}
