<?php

namespace App\Filament\Resources\WorkoutSessions\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Functional\Tracking\Enums\WorkoutSessionStatus;
use Illuminate\Database\Eloquent\Builder;

/**
 * Table des séances : utilisateur, statut, début, volume total.
 * Lecture seule + suppression : les séances sont loguées par l'app, jamais créées/éditées ici.
 */
class WorkoutSessionsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('user.name')
                    ->label('Utilisateur')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('title')
                    ->label('Titre')
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true)
                    ->placeholder('—'),
                TextColumn::make('status')
                    ->label('Statut')
                    ->badge()
                    ->color(fn (WorkoutSessionStatus $state): string => match ($state) {
                        WorkoutSessionStatus::Active => 'warning',
                        WorkoutSessionStatus::Completed => 'success',
                        WorkoutSessionStatus::Abandoned => 'danger',
                    }),
                TextColumn::make('started_at')
                    ->label('Débutée le')
                    ->dateTime()
                    ->sortable(),
                TextColumn::make('total_volume')
                    ->label('Volume total')
                    ->numeric()
                    ->sortable()
                    ->placeholder('—'),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label('Statut')
                    ->options(WorkoutSessionStatus::class),
                Filter::make('started_at')
                    ->label('Période')
                    ->schema([
                        DatePicker::make('from')->label('Du'),
                        DatePicker::make('until')->label('Au'),
                    ])
                    ->query(function (Builder $query, array $data): Builder {
                        return $query
                            ->when($data['from'] ?? null, fn (Builder $q, string $date): Builder => $q->whereDate('started_at', '>=', $date))
                            ->when($data['until'] ?? null, fn (Builder $q, string $date): Builder => $q->whereDate('started_at', '<=', $date));
                    }),
            ])
            ->recordActions([
                ViewAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('started_at', 'desc');
    }
}
