<?php

namespace App\Filament\Widgets;

use App\Models\Booking;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class LatestBookingsTable extends BaseWidget
{
    protected static ?string $heading = 'Últimas reservas';

    protected static ?int $sort = 4;

    protected int | string | array $columnSpan = 1;

    public function table(Table $table): Table
    {
        return $table
            ->query(Booking::query()->with(['offer', 'ride'])->latest()->limit(5))
            ->paginated(false)
            ->columns([
                Tables\Columns\TextColumn::make('offer.title')
                    ->label('Oferta')
                    ->description(fn (Booking $record): string => $record->customer_name ?: ($record->ride?->pickup_label ?: 'Reserva directa'))
                    ->weight('semibold'),
                Tables\Columns\TextColumn::make('amount')
                    ->label('Importe')
                    ->money('EUR'),
                Tables\Columns\TextColumn::make('status')
                    ->label('Estado')
                    ->badge()
                    ->color(fn (?string $state): string => match ($state) {
                        'confirmed', 'completed' => 'success',
                        'pending' => 'warning',
                        'cancelled' => 'danger',
                        default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Creada')
                    ->since(),
            ]);
    }
}
