<?php

namespace App\Filament\Resources\Backtests\Tables;

use App\Models\Backtest;
use Filament\Actions\ViewAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class BacktestsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('model_version')
                    ->searchable()
                    ->limit(32)
                    ->tooltip(fn (Backtest $record) => strlen($record->model_version) > 32 ? $record->model_version : null),
                TextColumn::make('directional_accuracy')
                    ->label('Accuracy')
                    ->state(fn (Backtest $record): string => $record->directional_accuracy === null
                        ? '—'
                        : round((float) $record->directional_accuracy, 1).'%')
                    ->color(fn (Backtest $record) => self::accuracyColor($record))
                    ->sortable(),
                TextColumn::make('mae_pence')
                    ->label('MAE')
                    ->state(fn (Backtest $record): string => $record->mae_pence === null
                        ? '—'
                        : number_format((float) $record->mae_pence, 2).'p')
                    ->sortable(),
                IconColumn::make('leak_suspected')
                    ->label('Leak?')
                    ->boolean()
                    ->trueColor('danger'),
                TextColumn::make('eval_start')
                    ->date('d M Y')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('eval_end')
                    ->date('d M Y')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('ran_at')
                    ->dateTime('d M Y H:i')
                    ->sortable(),
            ])
            ->defaultSort('ran_at', 'desc')
            ->filters([
                Filter::make('leak_suspected')
                    ->label('Suspicious accuracy (leak suspected)')
                    ->toggle()
                    ->query(fn (Builder $query) => $query->where('leak_suspected', true)),
                Filter::make('below_ship_gate')
                    ->label('Below ship gate')
                    ->toggle()
                    ->query(fn (Builder $query) => $query->where('directional_accuracy', '<', 62)),
            ])
            ->recordActions([
                ViewAction::make(),
            ]);
    }

    protected static function accuracyColor(Backtest $record): ?string
    {
        if ($record->directional_accuracy === null) {
            return null;
        }

        $accuracy = (float) $record->directional_accuracy;

        if ($accuracy > 75 && $record->leak_suspected) {
            return 'danger';
        }

        if ($accuracy < 60) {
            return 'danger';
        }

        if ($accuracy < 62) {
            return 'warning';
        }

        if ($accuracy <= 75) {
            return 'success';
        }

        return null;
    }
}
