<?php

namespace App\Filament\Resources\Users\Tables;

use App\Models\User;
use Filament\Actions\Action;
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\Table;

class UsersTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->defaultSort('created_at', 'desc')
            ->columns([
                TextColumn::make('name')
                    ->label('Member')
                    ->description(fn (User $record): string => $record->email)
                    ->weight('bold')
                    ->searchable(['name', 'email'])
                    ->sortable(),

                TextColumn::make('roles.name')
                    ->label('Role')
                    ->badge()
                    ->color(fn (string $state): string => $state === 'admin' ? 'primary' : 'gray')
                    ->formatStateUsing(fn (string $state): string => ucfirst($state)),

                TextColumn::make('segment')
                    ->badge()
                    ->color('info')
                    ->placeholder('-')
                    ->toggleable(),

                TextColumn::make('country')
                    ->placeholder('-')
                    ->toggleable(),

                TextColumn::make('attempts_count')
                    ->label('Checks')
                    ->counts('attempts')
                    ->badge()
                    ->color('success')
                    ->alignCenter(),

                IconColumn::make('email_verified_at')
                    ->label('Verified')
                    ->boolean()
                    ->alignCenter(),

                TextColumn::make('last_login_at')
                    ->label('Last seen')
                    ->since()
                    ->placeholder('Never')
                    ->sortable(),

                TextColumn::make('created_at')
                    ->label('Joined')
                    ->since()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('roles')
                    ->label('Role')
                    ->relationship('roles', 'name')
                    ->multiple()
                    ->preload(),

                SelectFilter::make('segment')
                    ->options([
                        'individual' => 'You & Your Family',
                        'corporate' => 'Companies & Investors',
                        'diaspora' => 'The Diaspora',
                    ]),
            ])
            ->recordActions([
                Action::make('impersonate')
                    ->label('View as')
                    ->icon('heroicon-o-eye')
                    ->color('warning')
                    ->url(fn (User $record): string => route('impersonate.start', $record))
                    ->visible(fn (User $record): bool =>
                        auth()->user()?->hasRole('admin')
                        && ! $record->is(auth()->user())
                        && ! $record->hasRole('admin')
                    ),
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
