<?php

namespace App\Filament\Resources\ShippingMethods\Tables;

use App\Models\ShippingMethod;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;

class ShippingMethodsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->searchable()
                    ->sortable()
                    ->weight('semibold'),

                TextColumn::make('zone')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'inside_dhaka'  => 'success',
                        'outside_dhaka' => 'info',
                        default         => 'primary',
                    })
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        'inside_dhaka'  => 'Inside Dhaka',
                        'outside_dhaka' => 'Outside Dhaka',
                        default         => $state,
                    })
                    ->sortable(),

                TextColumn::make('pricing_type')
                    ->label('Pricing')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'weight_based' => 'warning',
                        default        => 'gray',
                    })
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        'weight_based' => '⚖️ Weight-Based',
                        default        => '📦 Flat Rate',
                    }),

                TextColumn::make('cost_display')
                    ->label('Cost / Rate')
                    ->getStateUsing(function (ShippingMethod $record): string {
                        if ($record->pricing_type === 'weight_based') {
                            $base  = '৳' . number_format((float)$record->base_cost,  0);
                            $perKg = '৳' . number_format((float)$record->per_kg_rate, 0) . '/kg';
                            return "{$base} base + {$perKg}";
                        }
                        return '৳' . number_format((float)$record->cost, 2);
                    }),

                TextColumn::make('estimated_days')
                    ->label('Est. Days')
                    ->suffix(' days')
                    ->placeholder('—')
                    ->sortable(),

                ToggleColumn::make('is_active')
                    ->label('Active'),

                TextColumn::make('created_at')
                    ->label('Created At')
                    ->dateTime()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('zone')
                    ->options([
                        'inside_dhaka'  => 'Inside Dhaka',
                        'outside_dhaka' => 'Outside Dhaka',
                    ]),
                SelectFilter::make('pricing_type')
                    ->label('Pricing Type')
                    ->options([
                        'flat'         => 'Flat Rate',
                        'weight_based' => 'Weight-Based',
                    ]),
                TernaryFilter::make('is_active')
                    ->label('Active Status'),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->actions([
                EditAction::make(),
            ])
            ->bulkActions([
                DeleteBulkAction::make(),
            ]);
    }
}
