<?php

namespace App\Filament\Resources\Integrations\Tables;

use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;
use Technical\Integrations\Enums\IntegrationStatus;
use Technical\Integrations\Models\Integration;

/**
 * Table des intégrations externes : utilisateur, fournisseur, état, dernière synchro.
 * Les tokens (access/refresh) ne sont JAMAIS affichés.
 */
class IntegrationsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('user.name')
                    ->label('Utilisateur')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('provider')
                    ->label('Fournisseur')
                    ->badge()
                    ->color(fn (string $state): string => match (strtolower($state)) {
                        'strava' => 'danger',
                        'garmin' => 'info',
                        default => 'gray',
                    })
                    ->searchable()
                    ->sortable(),
                TextColumn::make('external_user_id')
                    ->label('ID externe')
                    ->toggleable(isToggledHiddenByDefault: true)
                    ->placeholder('—'),
                TextColumn::make('status')
                    ->label('État')
                    ->badge()
                    ->color(fn (IntegrationStatus $state): string => match ($state) {
                        IntegrationStatus::Connected => 'success',
                        IntegrationStatus::Revoked => 'gray',
                        IntegrationStatus::Error => 'danger',
                    }),
                TextColumn::make('connected_at')
                    ->label('Connecté le')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('last_synced_at')
                    ->label('Dernière synchro')
                    ->dateTime()
                    ->sortable()
                    ->placeholder('Jamais'),
            ])
            ->filters([
                SelectFilter::make('provider')
                    ->label('Fournisseur')
                    ->options(fn (): array => self::distinctProviders()),
                SelectFilter::make('status')
                    ->label('État')
                    ->options(IntegrationStatus::class),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    // Révocation groupée : coupe l'accès sans supprimer l'historique de connexion.
                    BulkAction::make('revoke')
                        ->label('Révoquer')
                        ->icon(Heroicon::OutlinedBoltSlash)
                        ->color('danger')
                        ->requiresConfirmation()
                        ->action(function (Collection $records): void {
                            $records->each(fn (Integration $integration) => $integration->update([
                                'status' => IntegrationStatus::Revoked,
                            ]));
                        })
                        ->deselectRecordsAfterCompletion(),
                ]),
            ])
            ->defaultSort('last_synced_at', 'desc');
    }

    /**
     * Valeurs distinctes de la colonne "provider" pour alimenter le filtre.
     *
     * @return array<string, string>
     */
    private static function distinctProviders(): array
    {
        return Integration::query()
            ->distinct()
            ->orderBy('provider')
            ->pluck('provider', 'provider')
            ->all();
    }
}
