<?php

namespace App\Filament\Resources\LegalDocuments\Tables;

use App\Enums\LegalDocumentType;
use App\Filament\Resources\LegalDocuments\LegalDocumentResource;
use App\Models\LegalDocument;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
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 LegalDocumentsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('id')
                    ->label(__('admin.legal_documents.fields.id'))
                    ->copyable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('slug')
                    ->label(__('admin.legal_documents.fields.slug'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('type')
                    ->label(__('admin.legal_documents.fields.type'))
                    ->badge()
                    ->sortable(),
                TextColumn::make('version')
                    ->label(__('admin.legal_documents.fields.version'))
                    ->numeric()
                    ->sortable(),
                TextColumn::make('localized_title')
                    ->label(__('admin.legal_documents.fields.localized_title'))
                    ->state(fn (LegalDocument $record): string => (string) ($record->title['fr'] ?? collect($record->title)->first() ?? __('admin.legal_documents.placeholders.empty'))),
                IconColumn::make('is_active')
                    ->label(__('admin.legal_documents.fields.is_active'))
                    ->boolean()
                    ->sortable(),
                TextColumn::make('published_at')
                    ->label(__('admin.legal_documents.fields.published_at'))
                    ->dateTime()
                    ->sortable()
                    ->placeholder(__('admin.legal_documents.placeholders.empty')),
                TextColumn::make('updated_at')
                    ->label(__('admin.legal_documents.fields.updated_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('type')
                    ->label(__('admin.legal_documents.fields.type'))
                    ->options(LegalDocumentType::class),
                TernaryFilter::make('is_active')
                    ->label(__('admin.legal_documents.fields.is_active')),
            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make(),
                LegalDocumentResource::createNewVersionAction(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
