<?php

namespace App\Filament\Resources\Attributes\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;

class AttributesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->defaultSort('sort_order')
            ->columns([
                TextColumn::make('name')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('type')
                    ->badge()
                    ->color(fn($state) => match ($state) {
                        'select', 'multiselect' => 'info',
                        default => 'gray',
                    }),
                TextColumn::make('unit')
                    ->placeholder('—'),
                TextColumn::make('options_count')
                    ->counts('options')
                    ->label('Options'),
                TextColumn::make('categories_count')
                    ->counts('categories')
                    ->label('Categories'),
                IconColumn::make('is_filterable')
                    ->boolean(),
                IconColumn::make('is_required')
                    ->boolean(),
                TextColumn::make('sort_order')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('type')->options([
                    'select'      => 'Select',
                    'multiselect' => 'Multi-select',
                    'text'        => 'Text',
                    'number'      => 'Number',
                    'boolean'     => 'Yes / No',
                ]),
                TernaryFilter::make('is_filterable')->label('Filterable'),
                TernaryFilter::make('is_required')->label('Required'),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
