<?php

namespace App\Filament\Resources;

use App\Filament\Resources\RideResource\Pages;
use App\Models\Ride;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class RideResource extends Resource
{
    protected static ?string $model = Ride::class;

    protected static ?string $navigationIcon = 'heroicon-o-map';
    protected static ?string $navigationLabel = 'Trayectos';
    protected static ?string $navigationGroup = 'Funnel TAXILANZ';
    protected static ?string $modelLabel = 'Trayecto';
    protected static ?string $pluralModelLabel = 'Trayectos';
    protected static ?int $navigationSort = 2;

    public static function getNavigationBadge(): ?string
    {
        return (string) static::getModel()::count();
    }

    public static function getNavigationBadgeColor(): ?string
    {
        return 'primary';
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('uuid')->label('UUID')->required()->maxLength(36),
                Forms\Components\TextInput::make('pickup_label')->label('Recogida')->required()->maxLength(255),
                Forms\Components\TextInput::make('pickup_lat')->numeric()->default(null),
                Forms\Components\TextInput::make('pickup_lng')->numeric()->default(null),
                Forms\Components\TextInput::make('destination_label')->label('Destino')->required()->maxLength(255),
                Forms\Components\TextInput::make('destination_lat')->numeric()->default(null),
                Forms\Components\TextInput::make('destination_lng')->numeric()->default(null),
                Forms\Components\DateTimePicker::make('scheduled_for')->label('Programado para'),
                Forms\Components\TextInput::make('status')->label('Estado')->required(),
                Forms\Components\TextInput::make('eta_minutes')->label('ETA (min)')->numeric()->default(null),
                Forms\Components\TextInput::make('source_channel')->label('Canal')->required(),
                Forms\Components\TextInput::make('context_zone')->label('Zona')->maxLength(255)->default(null),
                Forms\Components\TextInput::make('locale')->maxLength(10)->default(null),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('created_at', 'desc')
            ->columns([
                Tables\Columns\TextColumn::make('pickup_label')
                    ->label('Trayecto')
                    ->searchable()
                    ->description(fn (Ride $record): string => '→ '.$record->destination_label)
                    ->weight('semibold'),
                Tables\Columns\TextColumn::make('source_channel')
                    ->label('Canal')
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => ucfirst($state ?: 'directo'))
                    ->color(fn (?string $state): string => match ($state) {
                        'hotel' => 'warning',
                        'app' => 'success',
                        'web' => 'info',
                        'reception' => 'gray',
                        default => 'primary',
                    }),
                Tables\Columns\TextColumn::make('context_zone')
                    ->label('Zona')
                    ->badge()
                    ->searchable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('status')
                    ->label('Estado')
                    ->badge()
                    ->color(fn (?string $state): string => match ($state) {
                        'confirmed' => 'success',
                        'pending' => 'warning',
                        'cancelled' => 'danger',
                        default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('eta_minutes')
                    ->label('ETA')
                    ->suffix(' min')
                    ->sortable(),
                Tables\Columns\TextColumn::make('scheduled_for')
                    ->label('Programado')
                    ->dateTime('d/m/Y H:i')
                    ->sortable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('uuid')
                    ->label('UUID')
                    ->searchable()
                    ->copyable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Creado')
                    ->since()
                    ->sortable(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListRides::route('/'),
            'create' => Pages\CreateRide::route('/create'),
            'edit' => Pages\EditRide::route('/{record}/edit'),
        ];
    }
}
