<?php

namespace App\Filament\Resources\Searches;

use App\Filament\NavigationGroup;
use App\Filament\Resources\Searches\Pages\ListSearches;
use App\Models\Search;
use Filament\Resources\Resource;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class SearchResource extends Resource
{
    protected static ?string $model = Search::class;

    protected static string|\UnitEnum|null $navigationGroup = NavigationGroup::Data;

    protected static ?string $navigationLabel = 'Searches';

    protected static ?int $navigationSort = 4;

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('searched_at')
                    ->label('Searched At')
                    ->dateTime('d M Y H:i')
                    ->sortable(),
                TextColumn::make('fuel_type')
                    ->label('Fuel Type')
                    ->badge(),
                TextColumn::make('results_count')
                    ->label('Results')
                    ->numeric()
                    ->sortable(),
                TextColumn::make('lowest_pence')
                    ->label('Lowest')
                    ->formatStateUsing(fn (int $state): string => number_format($state / 100, 1).'p')
                    ->sortable(),
                TextColumn::make('highest_pence')
                    ->label('Highest')
                    ->formatStateUsing(fn (int $state): string => number_format($state / 100, 1).'p')
                    ->sortable(),
                TextColumn::make('avg_pence')
                    ->label('Average')
                    ->formatStateUsing(fn (string $state): string => number_format((float) $state / 100, 1).'p')
                    ->sortable(),
                TextColumn::make('lat_bucket')
                    ->label('Area (lat/lng)')
                    ->formatStateUsing(fn (Search $record): string => $record->lat_bucket.', '.$record->lng_bucket)
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('ip_hash')
                    ->label('IP Hash')
                    ->limit(16)
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultSort('searched_at', 'desc')
            ->filters([
                SelectFilter::make('fuel_type')
                    ->options([
                        'E10' => 'E10',
                        'E5' => 'E5',
                        'B7_STANDARD' => 'B7 Standard',
                        'B7_PREMIUM' => 'B7 Premium',
                        'B10' => 'B10',
                        'HVO' => 'HVO',
                    ]),
            ])
            ->recordActions([])
            ->toolbarActions([]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ListSearches::route('/'),
        ];
    }
}
