<?php

namespace App\Filament\Widgets;

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

class LatestRidesTable extends BaseWidget
{
    protected static ?string $heading = 'Últimos trayectos';

    protected static ?int $sort = 3;

    protected int | string | array $columnSpan = 1;

    public function table(Table $table): Table
    {
        return $table
            ->query(Ride::query()->latest()->limit(5))
            ->paginated(false)
            ->columns([
                Tables\Columns\TextColumn::make('pickup_label')
                    ->label('Trayecto')
                    ->description(fn (Ride $record): string => '→ '.$record->destination_label)
                    ->weight('semibold'),
                Tables\Columns\TextColumn::make('status')
                    ->label('Estado')
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => ucfirst($state ?: 'pendiente'))
                    ->color(fn (?string $state): string => match ($state) {
                        'confirmed', 'completed' => 'success',
                        'pending' => 'warning',
                        'cancelled' => 'danger',
                        default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('eta_minutes')
                    ->label('ETA')
                    ->formatStateUsing(fn ($state): string => $state ? $state.' min' : '—'),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Creado')
                    ->since(),
            ]);
    }
}
