<?php

namespace App\Filament\Resources\PricelistFiles\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use App\Enums\PricelistFileStatusEnum;

class PricelistFilesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->poll('5s')
            ->columns([
                TextColumn::make('filename')
                    ->label('Fájlnév')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('supplier.name')
                    ->label('Beszállító')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('status')
                    ->label('Státusz')
                    ->badge()
                    ->formatStateUsing(fn (PricelistFileStatusEnum $state): string => $state->label())
                    ->color(fn (PricelistFileStatusEnum $state): string => match ($state) {
                        PricelistFileStatusEnum::todo => 'gray',
                        PricelistFileStatusEnum::inprogress => 'info',
                        PricelistFileStatusEnum::done => 'success',
                        PricelistFileStatusEnum::fail => 'danger',
                        PricelistFileStatusEnum::waiting_for_approval => 'primary',
                        PricelistFileStatusEnum::closed => 'warning',
                    })
                    ->sortable(),
                TextColumn::make('processing_current_step')
                    ->label('Aktuális lépés')
                    ->placeholder('Nincs adat'),
                TextColumn::make('processing_current_step_percentage')
                    ->label('Készültség')
                    ->suffix('%')
                    ->alignCenter(),
                TextColumn::make('created_at')
                    ->label('Feltöltve')
                    ->dateTime('Y.m.d H:i')
                    ->sortable(),
            ])
            ->filters([
                TrashedFilter::make(),
            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make()
                    ->visible(fn ($record) => $record->status === PricelistFileStatusEnum::fail),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
