<?php

namespace App\Filament\Resources\Domains\Tables;

use App\enum\DomainType;
use App\enum\ProviderType;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class DomainsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Domain')
                    ->searchable()
                    ->weight('medium')
                    ->icon('heroicon-o-globe-alt')
                    ->copyable()
                    ->copyMessage('Domain copied!')
                    ->copyMessageDuration(1500),
                ToggleColumn::make('is_active')
                    ->label('Active')
                    ->alignCenter(),
                TextColumn::make('domain_type')
                    ->label('Type')
                    ->formatStateUsing(fn ($state) => $state ? DomainType::tryFrom($state)?->getLabel() : '-')
                    ->badge()
                    ->color(fn ($state) => $state ? DomainType::tryFrom($state)?->getColor() : 'gray')
                    ->alignCenter(),
                TextColumn::make('provider_type')
                    ->label('Provider')
                    ->formatStateUsing(fn ($state) => $state ? ProviderType::tryFrom($state)?->getLabel() : '-')
                    ->badge()
                    ->color(fn ($state) => $state ? ProviderType::tryFrom($state)?->getColor() : 'gray')
                    ->alignCenter(),
                TextColumn::make('daily_mailbox_limit')
                    ->label('Daily Limit')
                    ->numeric()
                    ->formatStateUsing(fn ($state) => number_format($state))
                    ->alignCenter()
                    ->icon('heroicon-o-inbox'),
                TextColumn::make('last_used_at')
                    ->label('Last Used')
                    ->dateTime('M j, Y g:i A')
                    ->placeholder('Never')
                    ->sortable()
                    ->since()
                    ->alignCenter(),
                TextColumn::make('checked_at')
                    ->label('Checked')
                    ->dateTime('M j, Y')
                    ->placeholder('Never')
                    ->sortable()
                    ->since()
                    ->alignCenter()
                    ->toggleable(isToggledHiddenByDefault: true),

            ])
            ->filters([
                SelectFilter::make('domain_type')
                    ->label('Domain Type')
                    ->options(DomainType::class),
                SelectFilter::make('provider_type')
                    ->label('Provider Type')
                    ->options(ProviderType::class),
                SelectFilter::make('is_active')
                    ->label('Status')
                    ->options([
                        true => 'Active',
                        false => 'Inactive',
                    ]),
                TrashedFilter::make(),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ])
            ->emptyStateHeading('No domains found')
            ->emptyStateDescription('Get started by creating your first domain.')
            ->emptyStateActions([
                // Add create action if needed
            ])
            ->poll('60s')
            ->striped()
            ->defaultPaginationPageOption(10)
            ->paginated([10, 25, 50, 100])
            ->reorderable('sort_order')
            ->defaultSort('name');
    }
}
