<?php

namespace App\Mail\Admin;

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

class RefundIssuedAdmin extends BaseAdminNotification
{
    public function __construct(
        public Invoice $invoice,
        public int $amountMinor,
    ) {}

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

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

        return new Content(
            markdown: 'mail.admin.refund-issued',
            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,
                'refundAmount' => "{$major} {$this->invoice->currency}",
                'invoiceTotal' => number_format(((int) $this->invoice->amount_cents) / 100, 2)." {$this->invoice->currency}",
                'isPartial' => $isPartial,
                'invoiceId' => $this->invoice->id,
            ]),
        );
    }
}
