<?php

namespace App\Filament\Resources\Users\Tables;

use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Select;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Functional\Users\Enums\Units;
use Illuminate\Database\Eloquent\Collection;
use Spatie\Permission\Models\Role;

/**
 * Table des utilisateurs : identité, rôles, préférences d'affichage, date d'inscription.
 */
class UsersTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Nom')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('email')
                    ->label('Email')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('roles.name')
                    ->label('Rôles')
                    ->badge()
                    ->color('primary'),
                TextColumn::make('locale')
                    ->label('Langue')
                    ->badge()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('units')
                    ->label('Unités')
                    ->badge(),
                TextColumn::make('email_verified_at')
                    ->label('Email vérifié')
                    ->dateTime()
                    ->placeholder('Non vérifié')
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('created_at')
                    ->label('Inscrit le')
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([
                SelectFilter::make('roles')
                    ->label('Rôle')
                    ->relationship('roles', 'name'),
                SelectFilter::make('units')
                    ->label('Unités')
                    ->options(Units::class),
                TernaryFilter::make('email_verified_at')
                    ->label('Email vérifié')
                    ->nullable(),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    // Assignation groupée d'un rôle Spatie sans repasser par chaque fiche.
                    BulkAction::make('assignRole')
                        ->label('Assigner un rôle')
                        ->icon(Heroicon::OutlinedShieldCheck)
                        ->color('primary')
                        ->schema([
                            Select::make('role')
                                ->label('Rôle')
                                ->options(fn (): array => Role::query()->pluck('name', 'name')->all())
                                ->required(),
                        ])
                        ->action(function (Collection $records, array $data): void {
                            $records->each(fn ($user) => $user->assignRole($data['role']));
                        })
                        ->deselectRecordsAfterCompletion(),
                ]),
            ])
            ->defaultSort('created_at', 'desc');
    }
}
