<?php

namespace App\Filament\Resources\Integrations\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Technical\Integrations\Enums\IntegrationStatus;

/**
 * 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()
                    ->searchable(),
                TextColumn::make('status')
                    ->label('État')
                    ->badge()
                    ->color(fn (IntegrationStatus $state): string => match ($state) {
                        IntegrationStatus::Connected => 'success',
                        IntegrationStatus::Revoked => 'gray',
                        IntegrationStatus::Error => 'danger',
                    }),
                TextColumn::make('last_synced_at')
                    ->label('Dernière synchro')
                    ->dateTime()
                    ->sortable()
                    ->placeholder('Jamais'),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label('État')
                    ->options(IntegrationStatus::class),
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('last_synced_at', 'desc');
    }
}
