<?php

namespace App\Filament\Resources\FormQuestionGroups\Tables;

use App\Enums\FormType;
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 FormQuestionGroupsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('form_type')
                    ->label('Form')
                    ->badge()
                    ->formatStateUsing(fn ($state) => $state instanceof FormType ? $state->label() : $state)
                    ->color(fn ($state) => match ($state) {
                        FormType::Adoption => 'success',
                        FormType::Fostering => 'info',
                        FormType::Assistance => 'warning',
                        default => 'gray',
                    }),

                TextColumn::make('name')
                    ->label('Group Name')
                    ->searchable()
                    ->icon(fn ($record) => $record->icon)
                    ->iconColor('gray'),

                TextColumn::make('description')
                    ->label('Description')
                    ->limit(50)
                    ->placeholder('No description'),

                TextColumn::make('questions_count')
                    ->label('Questions')
                    ->counts('questions')
                    ->badge()
                    ->color('gray'),

                IconColumn::make('is_active')
                    ->label('Active')
                    ->boolean(),
            ])
            ->defaultSort(function ($query) {
                return $query->orderBy('form_type', 'asc')->orderBy('sort_order', 'asc');
            })
            ->filters([
                SelectFilter::make('form_type')
                    ->label('Form Type')
                    ->options(collect(FormType::cases())->mapWithKeys(fn ($case) => [$case->value => $case->label()])),

                TernaryFilter::make('is_active')
                    ->label('Active'),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->reorderable('sort_order');
    }
}
