<?php

namespace App\Filament\Resources\Bookings\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class BookingsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('rider.name')
                    ->label('Rider')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('trip.fromCity.name_en')
                    ->label('From')
                    ->searchable(),

                TextColumn::make('trip.toCity.name_en')
                    ->label('To')
                    ->searchable(),

                TextColumn::make('trip.departure_at')
                    ->label('Departure')
                    ->dateTime('d M Y, H:i')
                    ->sortable(),

                TextColumn::make('seats_booked')
                    ->label('Seats')
                    ->numeric()
                    ->sortable(),

                TextColumn::make('trip.price_per_seat')
                    ->label('Price/Seat')
                    ->money('TZS', locale: 'en'),

                TextColumn::make('status')
                    ->badge()
                    ->color(fn(string $state): string => match ($state) {
                        'confirmed' => 'success',
                        'pending'   => 'warning',
                        'completed' => 'gray',
                        'cancelled' => 'danger',
                        'no_show'   => 'danger',
                        default     => 'primary',
                    }),

                TextColumn::make('confirmed_at')
                    ->label('Confirmed')
                    ->dateTime('d M Y H:i')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('cancellation_reason')
                    ->label('Cancel Reason')
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('created_at')
                    ->label('Booked At')
                    ->dateTime('d M Y H:i')
                    ->sortable(),
            ])
            ->defaultSort('created_at', 'desc')
            ->filters([
                SelectFilter::make('status')
                    ->options([
                        'pending'   => 'Pending',
                        'confirmed' => 'Confirmed',
                        'cancelled' => 'Cancelled',
                        'completed' => 'Completed',
                        'no_show'   => 'No-show',
                    ]),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
