<?php

namespace App\Mail;

use App\Models\ESign\EsignContract;
use App\Models\ESign\EsignSigner;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class EsignRequestMail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public EsignContract $contract,
        public EsignSigner $signer,
        public bool $isReminder = false,
    ) {}

    public function envelope(): Envelope
    {
        $prefix = $this->isReminder ? '[리마인더] ' : '';

        return new Envelope(
            from: new Address(config('mail.contact_from_address', config('mail.from.address')), config('mail.from.name')),
            subject: "{$prefix}[SAM] 전자계약 서명 요청 - {$this->contract->title}",
        );
    }

    public function content(): Content
    {
        $signUrl = config('app.url').'/esign/sign/'.$this->signer->access_token;

        return new Content(
            html: 'emails.esign.request',
            with: [
                'contractTitle' => $this->contract->title,
                'signerName' => $this->signer->name,
                'signUrl' => $signUrl,
                'expiresAt' => $this->contract->expires_at?->format('Y-m-d H:i'),
                'isReminder' => $this->isReminder,
            ],
        );
    }
}
