<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpNumberSequences\Tables;

use App\Business\SystemRule\NumberSequenceAllocator;
use App\Models\DberpNumberSequence;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;

class DberpNumberSequencesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->defaultSort('sort_order', 'asc')
            ->columns([
                TextColumn::make('code')
                    ->label(__('dberp.fields.rule_code'))
                    ->searchable(),
                TextColumn::make('name')
                    ->label(__('dberp.fields.rule_name'))
                    ->searchable(),
                TextColumn::make('applies_to')
                    ->label(__('dberp.fields.rule_applies_to'))
                    ->formatStateUsing(function (?string $state): string {
                        if ($state === null) {
                            return '';
                        }

                        $map = __('dberp.options.number_applies_to');

                        return is_array($map) ? (string) ($map[$state] ?? $state) : $state;
                    }),
                TextColumn::make('prefix')
                    ->label(__('dberp.fields.prefix')),
                TextColumn::make('date_segment')
                    ->label(__('dberp.fields.date_segment'))
                    ->formatStateUsing(function (?string $state): string {
                        if ($state === null) {
                            return '';
                        }

                        $map = __('dberp.options.date_segment');

                        return is_array($map) ? (string) ($map[$state] ?? $state) : $state;
                    }),
                TextColumn::make('serial_digits')
                    ->label(__('dberp.fields.serial_digits'))
                    ->numeric(),
                TextColumn::make('next_value')
                    ->label(__('dberp.fields.current_serial'))
                    ->numeric(),
                TextColumn::make('preview')
                    ->label(__('dberp.fields.number_preview'))
                    ->state(fn (DberpNumberSequence $record): string => app(NumberSequenceAllocator::class)->preview($record)),
                ToggleColumn::make('is_enabled')
                    ->label(__('dberp.fields.is_enabled')),
                TextColumn::make('sort_order')
                    ->label(__('dberp.fields.sort_order'))
                    ->numeric()
                    ->sortable(),
                TextColumn::make('updated_at')
                    ->label(__('dberp.fields.updated_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
            ]);
    }
}
