<?php

namespace App\Filament\Resources\DeliveryPartnerApplications\Tables;

use App\Models\DeliveryPartnerApplication;
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 DeliveryPartnerApplicationsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->defaultSort('created_at', 'desc')
            ->columns([
                TextColumn::make('name')->searchable()->weight('semibold'),
                TextColumn::make('phone')->searchable(),
                TextColumn::make('cnic')->searchable()->toggleable(),
                TextColumn::make('vehicle_type')->label('Vehicle')->badge(),
                TextColumn::make('vehicle_plate')->label('Plate')->searchable()->toggleable(),
                TextColumn::make('current_step')->label('Step')->badge()->sortable(),
                TextColumn::make('status')->badge()
                    ->color(fn($state) => match ($state) {
                        'draft' => 'gray', 'submitted' => 'warning', 'under_review' => 'info',
                        'approved' => 'success', 'rejected' => 'danger', default => 'gray',
                    })->sortable(),
                TextColumn::make('reviewed_at')->dateTime('M j, Y')->sortable()->toggleable(),
                TextColumn::make('created_at')->label('Applied')->dateTime('M j, Y H:i')->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')->options([
                    'draft'        => 'Draft',
                    'submitted'    => 'Submitted',
                    'under_review' => 'Under Review',
                    'approved'     => 'Approved',
                    'rejected'     => 'Rejected',
                ])->multiple(),
                SelectFilter::make('vehicle_type')->label('Vehicle')
                    ->options(fn() => DeliveryPartnerApplication::query()->whereNotNull('vehicle_type')->distinct()->pluck('vehicle_type', 'vehicle_type')->all()),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
