<?php

namespace App\Filament\Resources\Airlines\Tables;

use App\Models\Airline;
use App\Models\File;
use App\Services\FileService;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;

class AirlinesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('code')
                    ->label(__('flights.code'))
                    ->formatStateUsing(fn (Airline $record): string => ($record->iata ? $record->iata.'/' : '').$record->icao)
                    ->sortable()
                    ->searchable(['iata', 'icao']),

                TextColumn::make('name')
                    ->label(__(key: 'common.name'))
                    ->sortable()
                    ->searchable(),

                IconColumn::make('active')
                    ->label(__('common.active'))
                    ->boolean(),
            ])
            ->filters([
                TrashedFilter::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
                ForceDeleteAction::make()->before(function (Airline $record): void {
                    $record->files()->each(function (File $file): void {
                        app(FileService::class)->removeFile($file);
                    });
                }),
                RestoreAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make()->before(function (Collection $records): void {
                        /** @var Collection<int, Airline> $records */
                        $records->each(fn (Airline $record) => $record->files()->each(function (File $file): void {
                            app(FileService::class)->removeFile($file);
                        }));
                    }),
                    RestoreBulkAction::make(),
                ]),
            ])
            ->emptyStateActions([
            ]);
    }
}
