<?php

namespace Modules\Booking\Filament\Widgets;

use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Models\Booking;

class RecentBookingsTableWidget extends BaseWidget
{
    protected static ?int $sort = 2;

    protected int|string|array $columnSpan = 'full';

    public function table(Table $table): Table
    {
        return $table
            ->heading('Recent Bookings')
            ->query(
                Booking::query()
                    ->with(['route.fromDestination', 'route.toDestination'])
                    ->latest('created_at')
                    ->limit(10),
            )
            ->columns([
                TextColumn::make('booking_ref')
                    ->label('Ref'),
                TextColumn::make('status')
                    ->badge()
                    ->color(fn (BookingStatus $state) => match ($state) {
                        BookingStatus::PendingPayment => 'warning',
                        BookingStatus::Confirmed => 'success',
                        BookingStatus::Cancelled => 'gray',
                        BookingStatus::Expired => 'danger',
                    }),
                TextColumn::make('route.fromDestination.name')
                    ->label('From'),
                TextColumn::make('route.toDestination.name')
                    ->label('To'),
                TextColumn::make('travel_date')
                    ->date(),
                TextColumn::make('price')
                    ->numeric(2),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->since(),
            ])
            ->paginated(false);
    }
}
