<?php

namespace App\Filament\Resources;

use App\Filament\Resources\EventResource\Pages;
use App\Models\Event;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class EventResource extends Resource
{
    protected static ?string $model = Event::class;

    protected static ?string $navigationIcon = 'heroicon-o-bolt';
    protected static ?string $navigationLabel = 'Eventos';
    protected static ?string $navigationGroup = 'Funnel TAXILANZ';
    protected static ?string $modelLabel = 'Evento';
    protected static ?string $pluralModelLabel = 'Eventos';
    protected static ?int $navigationSort = 4;

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('event_type')->label('Tipo')->required(),
                Forms\Components\Select::make('ride_id')->label('Trayecto')->relationship('ride', 'id')->default(null),
                Forms\Components\Select::make('offer_id')->label('Oferta')->relationship('offer', 'title')->default(null),
                Forms\Components\Select::make('booking_id')->label('Reserva')->relationship('booking', 'id')->default(null),
                Forms\Components\TextInput::make('ride_recommendation_id')->label('Recomendación')->numeric()->default(null),
                Forms\Components\TextInput::make('session_id')->label('Sesión')->maxLength(255)->default(null),
                Forms\Components\Textarea::make('meta')->label('Meta')->columnSpanFull(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('created_at', 'desc')
            ->columns([
                Tables\Columns\TextColumn::make('event_type')
                    ->label('Evento')
                    ->badge()
                    ->formatStateUsing(fn (?string $state): string => str($state ?: 'unknown')->replace('_', ' ')->title())
                    ->color(fn (?string $state): string => match ($state) {
                        'request_created' => 'info',
                        'recommendation_viewed' => 'warning',
                        'recommendation_clicked' => 'primary',
                        'booking_started' => 'gray',
                        'booking_completed' => 'success',
                        default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('ride_id')
                    ->label('Ride')
                    ->badge()
                    ->color('info'),
                Tables\Columns\TextColumn::make('offer.title')
                    ->label('Oferta')
                    ->searchable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('booking_id')
                    ->label('Booking')
                    ->badge()
                    ->color('success')
                    ->toggleable(),
                Tables\Columns\TextColumn::make('ride_recommendation_id')
                    ->label('Top')
                    ->badge()
                    ->color('primary')
                    ->toggleable(),
                Tables\Columns\TextColumn::make('session_id')
                    ->label('Sesión')
                    ->limit(18)
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Creado')
                    ->dateTime('d/m/Y H:i')
                    ->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\ListEvents::route('/'),
            'create' => Pages\CreateEvent::route('/create'),
            'edit' => Pages\EditEvent::route('/{record}/edit'),
        ];
    }
}
