<?php

namespace Modules\Routing\Filament\Resources\EvRoutes\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Support\Enums\Width;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Modules\Routing\Models\EvRoute;
use Modules\Routing\Models\RoutePricing;

class EvRoutesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn ($query) => $query->with('pricing'))
            ->columns([
                TextColumn::make('company.name')
                    ->label('EV Company')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('fromDestination.name')
                    ->label('From')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('toDestination.name')
                    ->label('To')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('pricing')
                    ->label('Pricing')
                    ->state(fn (EvRoute $record) => $record->pricing
                        ->sortBy(fn (RoutePricing $pricing) => $pricing->vehicle_option->value)
                        ->map(fn (RoutePricing $pricing) => str($pricing->vehicle_option->value)->headline()
                            .': '
                            .($pricing->is_blocked ? 'Blocked' : number_format($pricing->price, 0)))
                        ->all())
                    ->listWithLineBreaks(),
                IconColumn::make('is_active')
                    ->boolean(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('ev_company_id')
                    ->label('Company')
                    ->relationship('company', 'name')
                    ->searchable()
                    ->preload(),
                SelectFilter::make('from_destination_id')
                    ->label('From')
                    ->relationship('fromDestination', 'name')
                    ->searchable()
                    ->preload(),
                SelectFilter::make('to_destination_id')
                    ->label('To')
                    ->relationship('toDestination', 'name')
                    ->searchable()
                    ->preload(),
                TernaryFilter::make('is_active'),
            ])
            ->filtersFormColumns(2)
            ->filtersFormWidth(Width::Large)
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
