<?php

namespace App\Filament\Resources\AssessmentAttempts\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class AssessmentAttemptsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->defaultSort('completed_at', 'desc')
            ->columns([
                TextColumn::make('assessment.title')
                    ->label('Assessment')
                    ->searchable(),
                TextColumn::make('user.name')
                    ->label('Member')
                    ->placeholder('Guest')
                    ->searchable(),
                TextColumn::make('guest_email')
                    ->label('Guest email')
                    ->placeholder('-')
                    ->searchable(),
                TextColumn::make('percent')
                    ->label('Score')
                    ->suffix('%')
                    ->numeric(0)
                    ->sortable()
                    ->badge()
                    ->color(fn ($state) => $state >= 60 ? 'success' : ($state >= 35 ? 'warning' : 'danger')),
                TextColumn::make('band.label')
                    ->label('Result band')
                    ->badge()
                    ->color('info'),
                TextColumn::make('status')
                    ->badge()
                    ->color(fn (string $state) => $state === 'completed' ? 'success' : 'gray'),
                TextColumn::make('completed_at')
                    ->dateTime('j M Y H:i')
                    ->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->options([
                        'completed' => 'Completed',
                        'in_progress' => 'In progress',
                    ]),
                SelectFilter::make('assessment')
                    ->relationship('assessment', 'title'),
            ])
            ->recordActions([
                //
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
