<?php

namespace Modules\Booking\Filament\Resources\Bookings\Schemas;

use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Filament\Resources\Bookings\BookingResource;
use Modules\Payment\Enums\PaymentStatus;

class BookingInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Booking')
                    ->schema([
                        Grid::make(4)
                            ->schema([
                                TextEntry::make('booking_ref')->label('Ref'),
                                TextEntry::make('status')
                                    ->badge()
                                    ->color(fn (BookingStatus $state) => match ($state) {
                                        BookingStatus::PendingPayment => 'warning',
                                        BookingStatus::Confirmed => 'success',
                                        BookingStatus::Cancelled => 'gray',
                                        BookingStatus::Expired => 'danger',
                                    }),
                                TextEntry::make('created_by_channel')->badge(),
                                TextEntry::make('created_at')->dateTime(),
                            ]),
                    ])
                    ->columnSpanFull(),
                Section::make('Trip')
                    ->schema([
                        Grid::make(3)
                            ->schema([
                                TextEntry::make('route.company.name')->label('Company'),
                                TextEntry::make('route.fromDestination.name')->label('From'),
                                TextEntry::make('route.toDestination.name')->label('To'),
                                TextEntry::make('timeSlot.label')->label('Time Slot'),
                                TextEntry::make('travel_date')->date(),
                                TextEntry::make('is_round_trip')->label('Round Trip')->badge(),
                                TextEntry::make('is_return_leg')->label('Leg')->badge()
                                    ->formatStateUsing(fn (bool $state) => $state ? 'Return' : 'Outbound')
                                    ->visible(fn ($record) => $record->is_round_trip),
                                TextEntry::make('linkedBooking.booking_ref')->label('Linked Leg')
                                    ->visible(fn ($record) => $record->is_round_trip)
                                    ->url(fn ($record) => $record->linked_booking_id
                                        ? BookingResource::getUrl('view', ['record' => $record->linked_booking_id])
                                        : null),
                            ]),
                    ])
                    ->columnSpanFull(),
                Section::make('Vehicle Options')
                    ->schema([
                        RepeatableEntry::make('vehicleOptions')
                            ->label('')
                            ->schema([
                                Grid::make(4)
                                    ->schema([
                                        TextEntry::make('vehicle_option')->badge(),
                                        TextEntry::make('passenger_count'),
                                        TextEntry::make('unit_price')->numeric(2),
                                        TextEntry::make('line_total')->numeric(2),
                                    ]),
                            ]),
                        TextEntry::make('price')->label('Total Price')->numeric(2),
                    ])
                    ->columnSpanFull(),
                Section::make('Passenger')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                TextEntry::make('passenger_name'),
                                TextEntry::make('passenger_phone'),
                                TextEntry::make('notes')
                                    ->label('Customer Notes')
                                    ->placeholder('—')
                                    ->columnSpanFull(),
                            ]),
                    ])
                    ->columnSpanFull(),
                Section::make('Staff Remark')
                    ->description('Internal only — never shown to the customer. Set via the Remark action.')
                    ->schema([
                        TextEntry::make('remark')
                            ->label('')
                            ->placeholder('No remark yet.'),
                    ])
                    ->columnSpanFull(),
                Section::make('Pickup & Dropoff')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                TextEntry::make('pickup_address'),
                                TextEntry::make('dropoff_address'),
                                TextEntry::make('pickup_lat')->label('Pickup Lat')->placeholder('—'),
                                TextEntry::make('dropoff_lat')->label('Dropoff Lat')->placeholder('—'),
                                TextEntry::make('pickup_lng')->label('Pickup Lng')->placeholder('—'),
                                TextEntry::make('dropoff_lng')->label('Dropoff Lng')->placeholder('—'),
                            ]),
                    ])
                    ->columnSpanFull(),
                Section::make('Driver & Vehicle')
                    ->description('Filled in by staff once the booking is confirmed — see the Assign Driver action.')
                    ->schema([
                        Grid::make(4)
                            ->schema([
                                TextEntry::make('driver_name')->label('Driver')->placeholder('Not yet assigned'),
                                TextEntry::make('driver_phone')->label('Driver Phone')->placeholder('Not yet assigned'),
                                TextEntry::make('car_plate_number')->label('Car Plate')->placeholder('Not yet assigned'),
                                TextEntry::make('car_model')->label('Car Model')->placeholder('—'),
                            ]),
                    ])
                    ->columnSpanFull(),
                // A booking can have more than one payment attempt if an
                // earlier one failed and the customer retried (domain.md §1)
                // — full detail (gateway response, refunds) lives on the
                // Payment/Refund Filament resources (T5.13), this is just a
                // quick-glance summary from the booking side.
                Section::make('Payments')
                    ->columnSpanFull()
                    ->schema([
                        RepeatableEntry::make('payments')
                            ->label('')
                            ->schema([
                                Grid::make(8)
                                    ->schema([
                                        TextEntry::make('gateway')->badge(),
                                        TextEntry::make('status')
                                            ->badge()
                                            ->color(fn (PaymentStatus $state) => match ($state) {
                                                PaymentStatus::Pending => 'warning',
                                                PaymentStatus::Completed => 'success',
                                                PaymentStatus::Failed => 'danger',
                                            }),
                                        TextEntry::make('amount')->numeric(2),
                                        TextEntry::make('currency'),
                                        TextEntry::make('gateway_transaction_id')->label('Gateway Txn ID')->placeholder('—'),
                                        TextEntry::make('gateway_payload.mm_order_id')->label('Transaction ID')->placeholder('—')->columnSpan(2),
                                        TextEntry::make('completed_at')->dateTime()->placeholder('—'),
                                    ]),
                            ])
                            ->placeholder('No payment attempts yet.'),
                    ]),
            ]);
    }
}
