@php
    /** @var \App\Models\Quotation|\App\Models\Invoice $document */
    $isInvoice = $document instanceof \App\Models\Invoice;
    $infoTitle = $isInvoice ? 'Invoice Information' : 'Quotation Information';
    $booking = $document->booking;
    $spaces = $booking?->spaces ?? collect();
    $packageItems = $document->items->filter(fn ($item) => $item->package_id);
    $customItems = $document->items->filter(fn ($item) => ! $item->package_id);
    $spacesSubtotal = (float) $spaces->sum('line_total');
    $chargesSubtotal = (float) $document->items->sum('line_total');
@endphp

<x-card :title="$infoTitle">
    <dl class="row mb-0">
        <dt class="col-sm-4 text-muted">Status</dt>
        <dd class="col-sm-8"><x-badge :status="$document->status->value" :label="$document->status->label()" /></dd>
        <dt class="col-sm-4 text-muted">Customer</dt>
        <dd class="col-sm-8">{{ $document->customer->name ?? '—' }}</dd>

        @if ($isInvoice)
            <dt class="col-sm-4 text-muted">Invoice Date</dt>
            <dd class="col-sm-8"><x-datetime :value="$document->invoice_date" /></dd>
            <dt class="col-sm-4 text-muted">Due Date</dt>
            <dd class="col-sm-8"><x-datetime :value="$document->due_date" /></dd>
        @else
            <dt class="col-sm-4 text-muted">Quotation Date</dt>
            <dd class="col-sm-8"><x-datetime :value="$document->quotation_date" /></dd>
            <dt class="col-sm-4 text-muted">Expires</dt>
            <dd class="col-sm-8"><x-datetime :value="$document->expires_at" /></dd>
            <dt class="col-sm-4 text-muted">Version</dt>
            <dd class="col-sm-8">v{{ $document->version }}</dd>
        @endif

        <dt class="col-sm-4 text-muted">Guest Count</dt>
        <dd class="col-sm-8">{{ $document->guest_count }}</dd>

        @if ($booking)
            <dt class="col-sm-4 text-muted">Booking</dt>
            <dd class="col-sm-8"><a href="{{ route('bookings.show', $booking) }}">{{ $booking->booking_number }}</a></dd>
            <dt class="col-sm-4 text-muted">Event</dt>
            <dd class="col-sm-8">{{ $booking->event_name ?? '—' }}</dd>
            @if ($booking->eventType)
                <dt class="col-sm-4 text-muted">Event Type</dt>
                <dd class="col-sm-8">{{ $booking->eventType->name }}</dd>
            @endif
            <dt class="col-sm-4 text-muted">Event Period</dt>
            <dd class="col-sm-8">
                <x-datetime :value="$booking->event_start" />
                —
                <x-datetime :value="$booking->event_end" />
            </dd>
            @if ($booking->coordinator)
                <dt class="col-sm-4 text-muted">Coordinator</dt>
                <dd class="col-sm-8">{{ $booking->coordinator->name }}</dd>
            @endif
            @if ($booking->contact_name || $booking->contact_phone || $booking->contact_email)
                <dt class="col-sm-4 text-muted">Contact</dt>
                <dd class="col-sm-8">
                    {{ $booking->contact_name }}
                    @if ($booking->contact_phone)
                        <span class="text-muted">· {{ $booking->contact_phone }}</span>
                    @endif
                    @if ($booking->contact_email)
                        <span class="text-muted">· {{ $booking->contact_email }}</span>
                    @endif
                </dd>
            @endif
            @if ($booking->description)
                <dt class="col-sm-4 text-muted">Description</dt>
                <dd class="col-sm-8">{{ $booking->description }}</dd>
            @endif
            @if ($booking->customer_notes)
                <dt class="col-sm-4 text-muted">Customer Notes</dt>
                <dd class="col-sm-8">{{ $booking->customer_notes }}</dd>
            @endif
            @if ($booking->internal_notes)
                <dt class="col-sm-4 text-muted">Internal Notes</dt>
                <dd class="col-sm-8">{{ $booking->internal_notes }}</dd>
            @endif
        @endif
    </dl>
</x-card>

<x-card title="Spaces" class="mt-3">
    @if ($spaces->isEmpty())
        <p class="text-muted mb-0">No spaces assigned.</p>
    @else
        <div class="table-responsive">
            <table class="table table-sm mb-0">
                <thead>
                    <tr>
                        <th>Space</th>
                        <th>Layout</th>
                        <th>Event Period</th>
                        <th class="text-end">Guests</th>
                        <th class="text-end">Unit Price</th>
                        <th>Basis</th>
                        <th class="text-end">Total</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($spaces as $space)
                        @php
                            $layoutCapacity = $space->space?->layouts
                                ?->firstWhere('layout_name', $space->seating_layout)
                                ?->capacity;
                        @endphp
                        <tr>
                            <td>{{ $space->space->name ?? '—' }}</td>
                            <td>
                                @if ($space->seating_layout)
                                    {{ $space->seating_layout }}
                                    @if ($layoutCapacity)
                                        <span class="text-muted small">(capacity {{ $layoutCapacity }})</span>
                                    @endif
                                @else
                                    —
                                @endif
                            </td>
                            <td>
                                <x-datetime :value="$space->event_start" />
                                —
                                <x-datetime :value="$space->event_end" />
                            </td>
                            <td class="text-end">{{ $space->guest_count }}</td>
                            <td class="text-end"><x-money :amount="$space->unit_price" :currency="$document->currency" /></td>
                            <td>{{ $space->charge_basis->label() }}</td>
                            <td class="text-end"><x-money :amount="$space->line_total" :currency="$document->currency" /></td>
                        </tr>
                    @endforeach
                </tbody>
                <tfoot>
                    <tr>
                        <th colspan="6" class="text-end">Spaces Subtotal</th>
                        <th class="text-end"><x-money :amount="$spacesSubtotal" :currency="$document->currency" /></th>
                    </tr>
                </tfoot>
            </table>
        </div>
    @endif
</x-card>

<x-card title="Additional Charges" class="mt-3">
    @if ($document->items->isEmpty())
        <p class="text-muted mb-0">No additional charges.</p>
    @else
        @if ($packageItems->isNotEmpty())
            <h6 class="text-muted mb-2">Available packages</h6>
            <div class="table-responsive mb-3">
                <table class="table table-sm mb-0">
                    <thead>
                        <tr>
                            <th>Description</th>
                            <th class="text-end">Qty</th>
                            <th class="text-end">Unit Price</th>
                            <th>Basis</th>
                            <th class="text-end">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($packageItems as $item)
                            <tr>
                                <td>{{ $item->description }}</td>
                                <td class="text-end">{{ $item->quantity }}</td>
                                <td class="text-end"><x-money :amount="$item->unit_price" :currency="$document->currency" /></td>
                                <td>{{ $item->charge_basis->label() }}</td>
                                <td class="text-end"><x-money :amount="$item->line_total" :currency="$document->currency" /></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif

        @if ($customItems->isNotEmpty())
            <h6 class="text-muted mb-2">Custom package</h6>
            <div class="table-responsive">
                <table class="table table-sm mb-0">
                    <thead>
                        <tr>
                            <th>Description</th>
                            <th class="text-end">Qty</th>
                            <th class="text-end">Unit Price</th>
                            <th>Basis</th>
                            <th class="text-end">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($customItems as $item)
                            <tr>
                                <td>{{ $item->description }}</td>
                                <td class="text-end">{{ $item->quantity }}</td>
                                <td class="text-end"><x-money :amount="$item->unit_price" :currency="$document->currency" /></td>
                                <td>{{ $item->charge_basis->label() }}</td>
                                <td class="text-end"><x-money :amount="$item->line_total" :currency="$document->currency" /></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif

        <div class="d-flex justify-content-end mt-2">
            <div class="text-end">
                <span class="text-muted small">Additional Charges Subtotal</span>
                <div class="fw-semibold"><x-money :amount="$chargesSubtotal" :currency="$document->currency" /></div>
            </div>
        </div>
    @endif
</x-card>

<x-card title="Totals" class="mt-3">
    <div class="table-responsive">
        <table class="table table-sm mb-0">
            <tbody>
                @if ($spaces->isNotEmpty())
                    <tr>
                        <th class="text-end text-muted fw-normal">Spaces</th>
                        <td class="text-end" style="width: 12rem;"><x-money :amount="$spacesSubtotal" :currency="$document->currency" /></td>
                    </tr>
                @endif
                @if ($document->items->isNotEmpty())
                    <tr>
                        <th class="text-end text-muted fw-normal">Additional Charges</th>
                        <td class="text-end"><x-money :amount="$chargesSubtotal" :currency="$document->currency" /></td>
                    </tr>
                @endif
                <tr>
                    <th class="text-end">Subtotal</th>
                    <td class="text-end"><x-money :amount="$document->subtotal" :currency="$document->currency" /></td>
                </tr>
                @if ((float) $document->discount_amount > 0)
                    <tr>
                        <th class="text-end text-muted fw-normal">Discount</th>
                        <td class="text-end">-<x-money :amount="$document->discount_amount" :currency="$document->currency" /></td>
                    </tr>
                @endif
                @if ((float) $document->service_charge_amount > 0)
                    <tr>
                        <th class="text-end text-muted fw-normal">Service Charge</th>
                        <td class="text-end"><x-money :amount="$document->service_charge_amount" :currency="$document->currency" /></td>
                    </tr>
                @endif
                <tr>
                    <th class="text-end">Tax</th>
                    <td class="text-end"><x-money :amount="$document->tax_amount" :currency="$document->currency" /></td>
                </tr>
                <tr>
                    <th class="text-end fs-5">Grand Total</th>
                    <td class="text-end fs-5 fw-bold text-primary"><x-money :amount="$document->grand_total" :currency="$document->currency" /></td>
                </tr>
                @if ($isInvoice)
                    <tr>
                        <th class="text-end text-muted fw-normal">Amount Paid</th>
                        <td class="text-end"><x-money :amount="$document->amount_paid" :currency="$document->currency" /></td>
                    </tr>
                    <tr>
                        <th class="text-end text-muted fw-normal">Outstanding</th>
                        <td class="text-end"><x-money :amount="$document->outstanding_balance" :currency="$document->currency" /></td>
                    </tr>
                @elseif ((float) $document->deposit_required > 0)
                    <tr>
                        <th class="text-end text-muted fw-normal">Deposit Required</th>
                        <td class="text-end"><x-money :amount="$document->deposit_required" :currency="$document->currency" /></td>
                    </tr>
                @endif
            </tbody>
        </table>
    </div>
</x-card>
