<?php

namespace App\Mail\Admin;

use App\Models\Invoice;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

class PaymentFailedAdmin extends BaseAdminNotification
{
    public function __construct(public Invoice $invoice) {}

    public function envelope(): Envelope
    {
        return new Envelope(subject: $this->adminSubject('Payment failed'));
    }

    public function content(): Content
    {
        $sub = $this->invoice->subscription;
        $user = $sub?->user;
        $amountMajor = number_format(((int) $this->invoice->amount_cents) / 100, 2);

        return new Content(
            markdown: 'mail.admin.payment-failed',
            with: array_merge($this->commonData(), [
                'userName' => $user?->name ?: $sub?->user_email_snapshot ?: '(unknown user)',
                'userEmail' => $user?->email ?: $sub?->user_email_snapshot ?: '(no email)',
                'planName' => $sub?->plan?->name ?: 'unknown plan',
                'providerKey' => $this->invoice->provider_key,
                'amount' => "{$amountMajor} {$this->invoice->currency}",
                'invoiceId' => $this->invoice->id,
                'hostedUrl' => $this->invoice->hosted_url,
            ]),
        );
    }
}
