<?php

namespace App\Mail;

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

class DeclarationTextMessageMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @param  string|null  $token  When set, recipient is client (use token-based URL). When null, recipient is comptable (use declarations.show).
     */
    public function __construct(
        public Declaration $declaration,
        public string $body,
        public ?string $token = null
    ) {}

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Nouveau message - '.$this->declaration->title,
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        $messagesUrl = $this->token
            ? route('client.upload', ['token' => $this->token])
            : route('declarations.show', ['declaration' => $this->declaration]).'?tab=messages';

        return new Content(
            markdown: 'emails.declaration-text-message',
            with: [
                'declarationTitle' => $this->declaration->title,
                'body' => $this->body,
                'messagesUrl' => $messagesUrl,
            ]
        );
    }
}
