<?php

namespace App\Mail;

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

class PurchaseConfirmation extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(public PackagePurchase $purchase)
    {
        //
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: __('emails.purchase.subject', ['package' => $this->localizedPackageName()]),
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.purchase',
            with: [
                'purchase' => $this->purchase,
                'user' => $this->purchase->tenant->user,
                'package' => $this->purchase->package,
                'packageName' => $this->localizedPackageName(),
            ],
        );
    }

    public function attachments(): array
    {
        return [];
    }

    private function localizedPackageName(): string
    {
        $locale = $this->locale ?? app()->getLocale();

        return optional($this->purchase->package)->getNameForLocale($locale) ?? '';
    }
}
