<?php

namespace App\Filament\Resources\TrialExtensions\Tables;

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

class TrialExtensionsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('user.name')
                    ->label('User')
                    ->searchable()
                    ->sortable()
                    ->limit(30),

                TextColumn::make('subscription.plan.name')
                    ->label('Plan')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('extension_days')
                    ->label('Days Extended')
                    ->sortable()
                    ->alignCenter()
                    ->badge()
                    ->color('success'),

                TextColumn::make('extension_type')
                    ->badge()
                    ->label('Type')
                    ->colors([
                        'blue' => 'manual',
                        'green' => 'automatic',
                        'orange' => 'compensation',
                    ]),

                TextColumn::make('original_trial_ends_at')
                    ->label('Original End')
                    ->dateTime('M j, Y')
                    ->sortable()
                    ->toggleable(),

                TextColumn::make('new_trial_ends_at')
                    ->label('New End')
                    ->dateTime('M j, Y')
                    ->sortable()
                    ->color('success')
                    ->description(fn ($record): string => $record->new_trial_ends_at->diffForHumans()
                    ),

                TextColumn::make('reason')
                    ->label('Reason')
                    ->limit(30)
                    ->toggleable(),

                TextColumn::make('grantedByAdmin.name')
                    ->label('Granted By')
                    ->searchable()
                    ->sortable()
                    ->toggleable(),

                TextColumn::make('granted_at')
                    ->label('Granted At')
                    ->dateTime('M j, Y')
                    ->sortable(),
            ])
            ->filters([
                SelectFilter::make('extension_type')
                    ->label('Extension Type')
                    ->options([
                        'manual' => 'Manual Grant',
                        'automatic' => 'Automatic Extension',
                        'compensation' => 'Compensation',
                    ]),

                SelectFilter::make('granted_by_admin_id')
                    ->label('Granted By')
                    ->relationship('grantedByAdmin', 'name')
                    ->searchable()
                    ->preload(),
            ])
            ->recordActions([
                EditAction::make(),

                Action::make('view_subscription')
                    ->label('View Subscription')
                    ->icon('heroicon-o-rectangle-stack')
                    ->color('blue')
                    ->url(fn ($record) => route('filament.' . filament()->getCurrentPanel()->getId() . '.resources.subscriptions.edit', $record->subscription_id))
                    ->openUrlInNewTab(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->emptyStateActions([
                CreateAction::make(),
            ]);
    }
}
