@extends('layouts.app')

@section('title', 'View Booking')

@section('content')
    <x-breadcrumbs :items="[
        ['label' => 'Dashboard', 'url' => route('dashboard')],
        ['label' => 'Bookings & Reservations', 'url' => route('bookings.index')],
        ['label' => $booking->booking_number],
    ]" />

    <x-page-header :title="$booking->event_name" :subtitle="$booking->booking_number">
        <div class="d-flex flex-wrap gap-2">
            @can('update', $booking)
                <a href="{{ route('bookings.edit', $booking) }}" class="btn btn-outline-primary btn-sm">Edit</a>
            @endcan
            @can('duplicate', $booking)
                <form method="POST" action="{{ route('bookings.duplicate', $booking) }}" class="d-inline">
                    @csrf
                    <button type="submit" class="btn btn-outline-secondary btn-sm">Duplicate</button>
                </form>
            @endcan
            @can('placeHold', $booking)
                @if (in_array($booking->status, [\App\Enums\BookingStatus::Enquiry, \App\Enums\BookingStatus::Provisional]))
                    <form method="POST" action="{{ route('bookings.place-hold', $booking) }}" class="d-inline">
                        @csrf
                        <button type="submit" class="btn btn-warning btn-sm">Place Hold</button>
                    </form>
                @endif
            @endcan
            @can('confirm', $booking)
                @if ($booking->status !== \App\Enums\BookingStatus::Confirmed)
                    <form method="POST" action="{{ route('bookings.confirm', $booking) }}" class="d-inline">
                        @csrf
                        <button type="submit" class="btn btn-success btn-sm">Confirm</button>
                    </form>
                @endif
            @endcan
            @can('cancel', $booking)
                @if (! in_array($booking->status, [\App\Enums\BookingStatus::Cancelled, \App\Enums\BookingStatus::Completed, \App\Enums\BookingStatus::NoShow]))
                    <form method="POST" action="{{ route('bookings.cancel', $booking) }}" class="d-inline" onsubmit="return confirm('Cancel this booking?');">
                        @csrf
                        <button type="submit" class="btn btn-outline-danger btn-sm">Cancel</button>
                    </form>
                @endif
            @endcan
        </div>
    </x-page-header>

    <div class="row g-3">
        <div class="col-lg-8">
            <x-card title="Overview">
                <dl class="row mb-0">
                    <dt class="col-sm-4 text-muted">Booking Number</dt>
                    <dd class="col-sm-8">{{ $booking->booking_number }}</dd>

                    <dt class="col-sm-4 text-muted">Status</dt>
                    <dd class="col-sm-8"><x-badge :status="$booking->status->value" :label="$booking->status->label()" /></dd>

                    <dt class="col-sm-4 text-muted">Customer</dt>
                    <dd class="col-sm-8">{{ $booking->customer->name }}</dd>

                    <dt class="col-sm-4 text-muted">Event Period</dt>
                    <dd class="col-sm-8">
                        <x-datetime :value="$booking->event_start" datetime />
                        —
                        <x-datetime :value="$booking->event_end" datetime />
                    </dd>

                    <dt class="col-sm-4 text-muted">Guests</dt>
                    <dd class="col-sm-8">{{ $booking->guest_count }}</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->description)
                        <dt class="col-sm-4 text-muted">Description</dt>
                        <dd class="col-sm-8">{{ $booking->description }}</dd>
                    @endif
                </dl>
            </x-card>

            <x-card title="Reserved Spaces" class="mt-3">
                @if ($booking->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>Period</th>
                                    <th>Reservation</th>
                                    <th class="text-end">Line Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($booking->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>
                                            <small>
                                                <x-datetime :value="$space->setup_start" datetime />
                                                →
                                                <x-datetime :value="$space->breakdown_end" datetime />
                                            </small>
                                        </td>
                                        <td>
                                            <x-badge :status="$space->reservation_status->value" :label="$space->reservation_status->label()" />
                                            @if ($space->hold_expires_at)
                                                <br><small class="text-muted">Hold until <x-datetime :value="$space->hold_expires_at" datetime /></small>
                                            @endif
                                        </td>
                                        <td class="text-end"><x-money :amount="$space->line_total" /></td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @endif
            </x-card>

            @php
                $chargeItems = $primaryInvoice?->items?->isNotEmpty()
                    ? $primaryInvoice->items
                    : $booking->items;
            @endphp

            @if ($chargeItems->isNotEmpty())
                <x-card title="Additional Charges" class="mt-3">
                    <div class="table-responsive">
                        <table class="table table-sm mb-0">
                            <thead>
                                <tr>
                                    <th>Description</th>
                                    <th>Qty</th>
                                    <th class="text-end">Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($chargeItems as $item)
                                    <tr>
                                        <td>{{ $item->description }}</td>
                                        <td>{{ $item->quantity }}</td>
                                        <td class="text-end"><x-money :amount="$item->line_total" /></td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    @if ($primaryInvoice)
                        <div class="mt-2 small text-muted">
                            From invoice
                            <a href="{{ route('invoices.show', $primaryInvoice) }}">{{ $primaryInvoice->invoice_number }}</a>
                        </div>
                    @endif
                </x-card>
            @endif
        </div>

        <div class="col-lg-4">
            @php
                $spacesSubtotal = (float) $booking->spaces->sum('line_total');
                $chargesSubtotal = (float) $chargeItems->sum('line_total');
                $summary = $primaryInvoice ?? $booking;
                $currency = $primaryInvoice?->currency ?? app(\App\Services\SettingsService::class)->get('currency', 'KES');
            @endphp
            <x-card title="Financial Summary" :subtitle="$primaryInvoice ? 'Matches '.$primaryInvoice->invoice_number : null">
                <dl class="row mb-0">
                    @if ($booking->spaces->isNotEmpty())
                        <dt class="col-6 text-muted">Spaces</dt>
                        <dd class="col-6 text-end"><x-money :amount="$spacesSubtotal" :currency="$currency" /></dd>
                    @endif
                    @if ($chargeItems->isNotEmpty())
                        <dt class="col-6 text-muted">Additional Charges</dt>
                        <dd class="col-6 text-end"><x-money :amount="$chargesSubtotal" :currency="$currency" /></dd>
                    @endif
                    <dt class="col-6 text-muted">Subtotal</dt>
                    <dd class="col-6 text-end"><x-money :amount="$summary->subtotal" :currency="$currency" /></dd>
                    @if ((float) $summary->discount_amount > 0)
                        <dt class="col-6 text-muted">Discount</dt>
                        <dd class="col-6 text-end">-<x-money :amount="$summary->discount_amount" :currency="$currency" /></dd>
                    @endif
                    @if ((float) $summary->service_charge_amount > 0)
                        <dt class="col-6 text-muted">Service Charge</dt>
                        <dd class="col-6 text-end"><x-money :amount="$summary->service_charge_amount" :currency="$currency" /></dd>
                    @endif
                    <dt class="col-6 text-muted">Tax</dt>
                    <dd class="col-6 text-end"><x-money :amount="$summary->tax_amount" :currency="$currency" /></dd>
                    <dt class="col-6"><strong>Grand Total</strong></dt>
                    <dd class="col-6 text-end"><strong class="text-primary"><x-money :amount="$summary->grand_total" :currency="$currency" /></strong></dd>
                    <dt class="col-6 text-muted">Amount Paid</dt>
                    <dd class="col-6 text-end"><x-money :amount="$summary->amount_paid" :currency="$currency" /></dd>
                    <dt class="col-6 text-muted">Outstanding</dt>
                    <dd class="col-6 text-end"><x-money :amount="$summary->outstanding_balance" :currency="$currency" /></dd>
                </dl>
                @if ($primaryInvoice)
                    <div class="mt-3">
                        <a href="{{ route('invoices.show', $primaryInvoice) }}" class="btn btn-outline-primary btn-sm w-100">
                            View Invoice {{ $primaryInvoice->invoice_number }}
                        </a>
                    </div>
                @endif
            </x-card>

            <x-card title="Actions" class="mt-3">
                <div class="d-grid gap-2">
                    @can('update', $booking)
                        @if ($booking->status === \App\Enums\BookingStatus::Confirmed)
                            <form method="POST" action="{{ route('bookings.mark-in-progress', $booking) }}">
                                @csrf
                                <button type="submit" class="btn btn-sm btn-outline-primary w-100">Mark In Progress</button>
                            </form>
                        @endif
                        @if ($booking->status === \App\Enums\BookingStatus::InProgress)
                            <form method="POST" action="{{ route('bookings.mark-completed', $booking) }}">
                                @csrf
                                <button type="submit" class="btn btn-sm btn-outline-success w-100">Mark Completed</button>
                            </form>
                            <form method="POST" action="{{ route('bookings.mark-no-show', $booking) }}">
                                @csrf
                                <button type="submit" class="btn btn-sm btn-outline-warning w-100">Mark No Show</button>
                            </form>
                        @endif
                        @if (in_array($booking->status, [\App\Enums\BookingStatus::Confirmed, \App\Enums\BookingStatus::AwaitingDeposit]))
                            <form method="POST" action="{{ route('bookings.postpone', $booking) }}">
                                @csrf
                                <button type="submit" class="btn btn-sm btn-outline-secondary w-100">Postpone</button>
                            </form>
                        @endif
                    @endcan
                    @can('delete', $booking)
                        <form method="POST" action="{{ route('bookings.destroy', $booking) }}" onsubmit="return confirm('Archive this booking?');">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-sm btn-outline-danger w-100">Archive</button>
                        </form>
                    @endcan
                </div>
            </x-card>
        </div>
    </div>

    <div class="row g-3 mt-0">
        <div class="col-12">
            <x-attachments-panel :attachable="$booking" />
        </div>
    </div>
@endsection
