<?php

namespace App\Filament\Resources;

use App\Filament\NavigationGroup;
use App\Filament\Resources\StationResource\Pages\ListStations;
use App\Filament\Resources\StationResource\Pages\ViewStation;
use App\Models\Station;
use Filament\Actions\ViewAction;
use Filament\Infolists\Components\IconEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;

class StationResource extends Resource
{
    protected static ?string $model = Station::class;

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

    protected static ?string $navigationLabel = 'Stations';

    protected static ?int $navigationSort = 1;

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('trading_name')->searchable()->sortable()
                    ->limit(25)
                    ->tooltip(fn (Station $record) => strlen($record->trading_name) > 45 ? $record->trading_name : null),
                TextColumn::make('brand_name')->searchable()->placeholder('—'),
                TextColumn::make('city')
                    ->description(fn (Station $record) => $record->postcode)
                    ->searchable(['city', 'postcode']),
                IconColumn::make('is_supermarket')->label('Supermarket')->boolean(),
                IconColumn::make('is_motorway_service_station')->label('Motorway')->boolean(),
                IconColumn::make('temporary_closure')
                    ->label('Temp closed')
                    ->boolean()
                    ->trueColor('warning')
                    ->falseColor('success'),
                TextColumn::make('last_seen_at')->dateTime('d M Y H:i')->sortable(),
            ])
            ->searchPlaceholder('Search name, brand, or postcode...')
            ->defaultSort('last_seen_at', 'desc')
            ->filters([
                TernaryFilter::make('is_supermarket')->label('Supermarket'),
                TernaryFilter::make('is_motorway_service_station')->label('Motorway'),
                TernaryFilter::make('temporary_closure')->label('Temporarily closed'),
                TernaryFilter::make('permanent_closure')->label('Permanently closed'),
            ])
            ->recordActions([
                ViewAction::make(),
            ]);
    }

    public static function infolist(Schema $schema): Schema
    {
        return $schema->components([
            Section::make('Location')->schema([
                TextEntry::make('trading_name'),
                TextEntry::make('brand_name')->placeholder('—'),
                TextEntry::make('address_line_1'),
                TextEntry::make('address_line_2')->placeholder('—'),
                TextEntry::make('city'),
                TextEntry::make('county')->placeholder('—'),
                TextEntry::make('postcode'),
                TextEntry::make('country'),
            ])->columns(3),
            Section::make('Status')->schema([
                IconEntry::make('is_supermarket')->boolean(),
                IconEntry::make('is_motorway_service_station')->boolean(),
                IconEntry::make('temporary_closure')->boolean()->trueColor('warning'),
                IconEntry::make('permanent_closure')->boolean()->trueColor('danger'),
                TextEntry::make('permanent_closure_date')->date()->placeholder('—'),
                TextEntry::make('last_seen_at')->dateTime('d M Y H:i'),
            ])->columns(3),
            Section::make('Fuel Types')->schema([
                TextEntry::make('fuel_types')
                    ->listWithLineBreaks()
                    ->columnSpanFull(),
            ]),
            Section::make('Amenities')->schema([
                TextEntry::make('amenities')
                    ->listWithLineBreaks()
                    ->placeholder('None recorded')
                    ->columnSpanFull(),
            ]),
        ]);
    }

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