<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Offers\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Maestro\Core\Models\Organization;

final class OfferSentMailable extends Mailable
{
    use Queueable, SerializesModels;

    /** @param  array<string, mixed>  $document */
    public function __construct(
        public Organization $organization,
        public array $document,
        public string $offerLinkUrl,
        public ?string $preSubject = null,
    ) {}

    public function envelope(): Envelope
    {
        $subject = $this->preSubject ?? sprintf(
            '%s — offer %s',
            $this->organization->company_name ?: $this->organization->name,
            $this->document['offer_number'] ?? ''
        );

        return new Envelope(subject: $subject);
    }

    public function content(): Content
    {
        return new Content(
            html: 'maestro-core::offers.mail.sent',
            with: [
                'organizationName' => $this->organization->display_name ?? $this->organization->company_name ?? $this->organization->name,
                'offerNumber' => (string) ($this->document['offer_number'] ?? ''),
                'bodyIntro' => (string) ($this->document['subject'] ?? ''),
                'offerLinkUrl' => $this->offerLinkUrl,
            ],
        );
    }
}
