<?php

namespace App\Filament\Resources\PaymentProviders\Tables;

use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\Action;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class PaymentProvidersTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Provider')
                    ->searchable()
                    ->sortable()
                    ->weight('bold'),

                TextColumn::make('display_name')
                    ->label('Display Name')
                    ->searchable()
                    ->sortable(),

                IconColumn::make('is_active')
                    ->label('Active')
                    ->boolean()
                    ->sortable(),

                IconColumn::make('supports_recurring')
                    ->label('Recurring')
                    ->boolean()
                    ->sortable(),

                IconColumn::make('supports_one_time')
                    ->label('One-Time')
                    ->boolean()
                    ->sortable(),

                IconColumn::make('is_fallback')
                    ->label('Fallback')
                    ->boolean()
                    ->sortable()
                    ->color(fn ($record) => $record->is_fallback ? 'warning' : null),

                TextColumn::make('priority')
                    ->label('Priority')
                    ->numeric()
                    ->sortable()
                    ->alignCenter(),

                TextColumn::make('supported_currencies')
                    ->label('Currencies')
                    ->formatStateUsing(fn ($state) => is_array($state) ? implode(', ', array_keys($state)) : '')
                    ->limitList(2)
                    ->separator(', ')
                    ->tooltip(fn ($record) => is_array($record->supported_currencies) ? implode(', ', array_keys($record->supported_currencies)) : ''),

                TextColumn::make('created_at')
                    ->label('Created')
                    ->dateTime('M j, Y')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),

                TextColumn::make('updated_at')
                    ->label('Updated')
                    ->dateTime('M j, Y')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('is_active')
                    ->label('Status')
                    ->options([
                        '1' => 'Active',
                        '0' => 'Inactive',
                    ]),

                SelectFilter::make('supports_recurring')
                    ->label('Recurring Support')
                    ->options([
                        '1' => 'Supports Recurring',
                        '0' => 'No Recurring Support',
                    ]),

                SelectFilter::make('supports_one_time')
                    ->label('One-Time Support')
                    ->options([
                        '1' => 'Supports One-Time',
                        '0' => 'No One-Time Support',
                    ]),

                SelectFilter::make('is_fallback')
                    ->label('Fallback Status')
                    ->options([
                        '1' => 'Is Fallback',
                        '0' => 'Not Fallback',
                    ]),
            ])
            ->recordActions([
                EditAction::make(),

                Action::make('test_connection')
                    ->label('Test Connection')
                    ->icon('heroicon-o-signal')
                    ->color('info')
                    ->action(function ($record) {
                        $result = $record->testConnection();

                        if ($result['success']) {
                            \Filament\Notifications\Notification::make()
                                ->title('Connection Test Successful')
                                ->body('Provider is configured and responding correctly.')
                                ->success()
                                ->send();
                        } else {
                            \Filament\Notifications\Notification::make()
                                ->title('Connection Test Failed')
                                ->body($result['error'])
                                ->danger()
                                ->send();
                        }
                    }),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),

                BulkAction::make('activate')
                    ->label('Activate Selected')
                    ->icon('heroicon-o-check')
                    ->action(function ($records) {
                        $records->each->update(['is_active' => true]);
                        \Filament\Notifications\Notification::make()
                            ->title('Providers Activated')
                            ->body('Selected payment providers have been activated.')
                            ->success()
                            ->send();
                    })
                    ->deselectRecordsAfterCompletion(),

                BulkAction::make('deactivate')
                    ->label('Deactivate Selected')
                    ->icon('heroicon-o-x-mark')
                    ->action(function ($records) {
                        $records->where('is_fallback', false)->each->update(['is_active' => false]);
                        \Filament\Notifications\Notification::make()
                            ->title('Providers Deactivated')
                            ->body('Selected payment providers have been deactivated (fallback providers were skipped).')
                            ->warning()
                            ->send();
                    })
                    ->deselectRecordsAfterCompletion(),
            ])
            ->emptyStateActions([
                CreateAction::make(),
            ])
            ->emptyStateDescription('No payment providers configured yet.')
            ->emptyStateHeading('No Payment Providers')
            ->emptyStateIcon('heroicon-o-rectangle-stack');
    }
}
