@php
    $isModel = $invoice instanceof \App\Models\Invoice;
    $modelItems = $isModel && $invoice->relationLoaded('items') ? $invoice->items : collect();

    if ($isModel) {
        $business = $invoice->business ?? $currentBusiness;
        $customer = $invoice->customer;
        $invoiceDisplay = [
            'number' => $invoice->invoice_number ?: 'INV-0000',
            'status' => $invoice->status_label ?: 'Draft',
            'statusValue' => $invoice->status_value ?: 'draft',
            'issueDate' => $invoice->issue_date?->format('M d, Y') ?? now()->format('M d, Y'),
            'dueDate' => $invoice->due_date?->format('M d, Y') ?? now()->addDays(7)->format('M d, Y'),
            'customer' => $customer?->name ?? 'Customer Name',
            'email' => $customer?->email ?? 'customer@example.com',
            'phone' => $customer?->phone ?? 'Phone not added',
            'business' => $business?->name ?? 'Booklio Business',
            'businessEmail' => $business?->email ?? 'billing@booklio.test',
            'businessPhone' => $business?->phone ?? '(555) 900-1200',
            'businessAddress' => $business?->address ?? 'Business address',
            'currency' => $invoice->currency ?: ($business?->currency ?? 'USD'),
            'subtotal' => (float) $invoice->subtotal,
            'tax' => (float) $invoice->tax_total,
            'discount' => (float) $invoice->discount_total,
            'total' => (float) $invoice->total,
            'paid' => (float) $invoice->paid_total,
            'balance' => (float) $invoice->balance_due,
            'notes' => $invoice->notes ?: $invoice->footer_note ?: 'Thank you for booking with us.',
        ];

        $items = $items ?? $modelItems->map(fn ($item) => [
            'name' => $item->service?->name ?? $item->description,
            'description' => $item->description,
            'qty' => $item->quantity,
            'price' => (float) $item->unit_price,
            'total' => (float) $item->line_total,
        ])->all();
    } else {
        $invoiceDisplay = array_merge([
            'number' => 'INV-0000',
            'status' => 'Draft',
            'statusValue' => 'draft',
            'issueDate' => now()->format('M d, Y'),
            'dueDate' => now()->addDays(7)->format('M d, Y'),
            'customer' => 'Customer Name',
            'email' => 'customer@example.com',
            'phone' => 'Phone not added',
            'business' => 'Booklio Business',
            'businessEmail' => 'billing@booklio.test',
            'businessPhone' => '(555) 900-1200',
            'businessAddress' => 'Business address',
            'currency' => 'USD',
            'subtotal' => 0,
            'tax' => 0,
            'discount' => 0,
            'total' => 0,
            'paid' => 0,
            'balance' => 0,
            'notes' => 'Thank you for booking with us.',
        ], $invoice ?? []);
    }

    $items = $items ?? [];

    if (count($items) === 0) {
        $items = [
            ['name' => 'Service appointment', 'description' => 'Invoice item preview', 'qty' => 1, 'price' => 0, 'total' => 0],
        ];
    }

    $money = fn ($amount) => $invoiceDisplay['currency'].' '.number_format((float) $amount, 2);

    $statusVariants = [
        'draft' => 'muted',
        'unpaid' => 'warning',
        'partial' => 'purple',
        'paid' => 'success',
        'overdue' => 'danger',
        'cancelled' => 'muted',
    ];
@endphp

<div class="mx-auto w-full max-w-3xl rounded-lg border border-booklio-border bg-white p-6 shadow-card sm:p-8">
    <div class="flex flex-col gap-5 border-b border-booklio-border pb-6 sm:flex-row sm:items-start sm:justify-between">
        <div class="min-w-0">
            <div class="flex items-center gap-3">
                <div class="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl bg-booklio-primary text-2xl font-extrabold text-white">B</div>
                <div class="min-w-0">
                    <p class="truncate text-xl font-extrabold text-booklio-dark">{{ $invoiceDisplay['business'] }}</p>
                    <p class="text-xs font-semibold text-booklio-muted">Premium appointment invoice</p>
                </div>
            </div>
            <div class="mt-5 min-w-0 space-y-1 break-words text-sm leading-6 text-slate-600">
                <p>{{ $invoiceDisplay['businessAddress'] }}</p>
                <p>{{ $invoiceDisplay['businessEmail'] }} - {{ $invoiceDisplay['businessPhone'] }}</p>
            </div>
        </div>
        <div class="text-left sm:text-right">
            <p class="text-3xl font-extrabold tracking-tight">INVOICE</p>
            <p class="mt-2 break-words text-sm font-bold text-booklio-muted">{{ $invoiceDisplay['number'] }}</p>
            <x-ui.badge :variant="$statusVariants[$invoiceDisplay['statusValue']] ?? 'muted'" class="mt-3">
                {{ $invoiceDisplay['status'] }}
            </x-ui.badge>
        </div>
    </div>

    <div class="grid gap-6 border-b border-booklio-border py-6 sm:grid-cols-2">
        <div class="min-w-0">
            <p class="text-xs font-bold uppercase tracking-wide text-booklio-muted">Bill To</p>
            <p class="mt-2 break-words text-base font-extrabold">{{ $invoiceDisplay['customer'] }}</p>
            <p class="mt-1 break-words text-sm font-semibold text-slate-600">{{ $invoiceDisplay['email'] }}</p>
            <p class="mt-1 break-words text-sm font-semibold text-slate-600">{{ $invoiceDisplay['phone'] }}</p>
        </div>
        <div class="grid gap-3 text-sm sm:text-right">
            <div>
                <p class="text-xs font-bold uppercase tracking-wide text-booklio-muted">Issue Date</p>
                <p class="mt-1 font-extrabold">{{ $invoiceDisplay['issueDate'] }}</p>
            </div>
            <div>
                <p class="text-xs font-bold uppercase tracking-wide text-booklio-muted">Due Date</p>
                <p class="mt-1 font-extrabold">{{ $invoiceDisplay['dueDate'] }}</p>
            </div>
        </div>
    </div>

    <div class="overflow-x-auto booklio-scrollbar">
        <table class="min-w-[760px] w-full divide-y divide-booklio-border">
            <thead>
                <tr>
                    <th class="py-4 pr-4 text-left text-xs font-bold uppercase tracking-wide text-booklio-muted">Item</th>
                    <th class="px-4 py-4 text-left text-xs font-bold uppercase tracking-wide text-booklio-muted">Qty</th>
                    <th class="px-4 py-4 text-right text-xs font-bold uppercase tracking-wide text-booklio-muted">Price</th>
                    <th class="py-4 pl-4 text-right text-xs font-bold uppercase tracking-wide text-booklio-muted">Total</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-booklio-border">
                @foreach ($items as $item)
                    <tr>
                        <td class="py-4 pr-4">
                            <p class="break-words text-sm font-extrabold">{{ $item['name'] }}</p>
                            <p class="mt-1 break-words text-xs font-medium text-booklio-muted">{{ $item['description'] }}</p>
                        </td>
                        <td class="px-4 py-4 text-sm font-bold">{{ $item['qty'] }}</td>
                        <td class="px-4 py-4 text-right text-sm font-bold">{{ $money($item['price']) }}</td>
                        <td class="py-4 pl-4 text-right text-sm font-extrabold">{{ $money($item['total']) }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>

    <div class="ml-auto mt-6 w-full max-w-sm space-y-3 text-sm">
        <div class="flex items-center justify-between gap-4"><span class="font-bold text-booklio-muted">Subtotal</span><span class="font-extrabold">{{ $money($invoiceDisplay['subtotal']) }}</span></div>
        <div class="flex items-center justify-between gap-4"><span class="font-bold text-booklio-muted">Tax</span><span class="font-extrabold">{{ $money($invoiceDisplay['tax']) }}</span></div>
        <div class="flex items-center justify-between gap-4"><span class="font-bold text-booklio-muted">Discount</span><span class="font-extrabold">{{ $money($invoiceDisplay['discount']) }}</span></div>
        <div class="border-t border-booklio-border pt-3">
            <div class="flex items-center justify-between gap-4 text-base"><span class="font-extrabold">Total</span><span class="font-extrabold">{{ $money($invoiceDisplay['total']) }}</span></div>
        </div>
        <div class="flex items-center justify-between gap-4"><span class="font-bold text-booklio-muted">Paid</span><span class="font-extrabold text-emerald-600">{{ $money($invoiceDisplay['paid']) }}</span></div>
        <div class="flex items-center justify-between gap-4"><span class="font-bold text-booklio-muted">Balance Due</span><span class="font-extrabold text-booklio-danger">{{ $money($invoiceDisplay['balance']) }}</span></div>
    </div>

    <div class="mt-8 rounded-lg bg-slate-50 p-4">
        <p class="text-xs font-bold uppercase tracking-wide text-booklio-muted">Notes</p>
        <p class="mt-2 break-words text-sm leading-6 text-slate-700">{{ $invoiceDisplay['notes'] }}</p>
    </div>
</div>
