<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BookingResource\Pages;
use App\Models\Booking;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class BookingResource extends Resource
{
    protected static ?string $model = Booking::class;

    protected static ?string $navigationIcon = 'heroicon-o-ticket';
    protected static ?string $navigationLabel = 'Reservas';
    protected static ?string $navigationGroup = 'Funnel TAXILANZ';
    protected static ?string $modelLabel = 'Reserva';
    protected static ?string $pluralModelLabel = 'Reservas';
    protected static ?int $navigationSort = 3;

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('uuid')->label('UUID')->required()->maxLength(36),
                Forms\Components\Select::make('ride_id')->label('Trayecto')->relationship('ride', 'id')->default(null),
                Forms\Components\Select::make('offer_id')->label('Oferta')->relationship('offer', 'title')->required(),
                Forms\Components\TextInput::make('ride_recommendation_id')->label('Recomendación')->numeric()->default(null),
                Forms\Components\TextInput::make('customer_name')->label('Cliente')->maxLength(255)->default(null),
                Forms\Components\TextInput::make('customer_email')->email()->maxLength(255)->default(null),
                Forms\Components\TextInput::make('customer_phone')->tel()->maxLength(255)->default(null),
                Forms\Components\TextInput::make('party_size')->label('Personas')->required()->numeric()->default(1),
                Forms\Components\DateTimePicker::make('booking_for')->label('Fecha / hora'),
                Forms\Components\TextInput::make('status')->label('Estado')->required(),
                Forms\Components\TextInput::make('amount')->label('Importe')->numeric()->default(null),
                Forms\Components\TextInput::make('commission_amount')->label('Comisión')->numeric()->default(null),
                Forms\Components\Textarea::make('notes')->label('Nota')->columnSpanFull(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('created_at', 'desc')
            ->columns([
                Tables\Columns\TextColumn::make('offer.title')
                    ->label('Oferta')
                    ->searchable()
                    ->weight('semibold'),
                Tables\Columns\TextColumn::make('customer_name')
                    ->label('Cliente')
                    ->searchable()
                    ->description(fn (Booking $record): string => $record->ride?->pickup_label ? 'Ride: '.$record->ride->pickup_label : 'Reserva directa'),
                Tables\Columns\TextColumn::make('status')
                    ->label('Estado')
                    ->badge()
                    ->color(fn (?string $state): string => match ($state) {
                        'confirmed', 'completed' => 'success',
                        'pending' => 'warning',
                        'cancelled' => 'danger',
                        default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('amount')
                    ->label('Importe')
                    ->money('EUR')
                    ->sortable(),
                Tables\Columns\TextColumn::make('commission_amount')
                    ->label('Comisión')
                    ->money('EUR')
                    ->sortable(),
                Tables\Columns\TextColumn::make('ride_recommendation_id')
                    ->label('Top')
                    ->formatStateUsing(fn ($state): string => $state ? 'Sí' : 'Directa')
                    ->badge()
                    ->color(fn ($state): string => $state ? 'primary' : 'gray'),
                Tables\Columns\TextColumn::make('booking_for')
                    ->label('Fecha')
                    ->dateTime('d/m/Y H:i')
                    ->sortable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Creada')
                    ->since()
                    ->sortable(),
                Tables\Columns\TextColumn::make('uuid')
                    ->label('UUID')
                    ->copyable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->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\ListBookings::route('/'),
            'create' => Pages\CreateBooking::route('/create'),
            'edit' => Pages\EditBooking::route('/{record}/edit'),
        ];
    }
}
