<?php

namespace App\Filament\Resources\TravelRequests\Tables;

use App\Enums\TravelStatus;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\BadgeColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class TravelRequestsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('id')->sortable(),
                TextColumn::make('user.name')->label('Applicant')->searchable()->sortable(),
                TextColumn::make('reason_summary')->limit(50)->label('Reason'),
                TextColumn::make('status')->badge()
                    ->color(fn (TravelStatus $state) => match($state) {
                        TravelStatus::Draft => 'gray',
                        TravelStatus::Pending => 'warning',
                        TravelStatus::Approved => 'success',
                        TravelStatus::Rejected => 'danger',
                    }),
                TextColumn::make('submitted_at')->dateTime()->sortable(),
                TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->options(collect(TravelStatus::cases())->mapWithKeys(fn ($e) => [$e->value => $e->value])->toArray()),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('created_at', 'desc');
    }
}
