<?php

namespace Modules\Routing\Filament\Resources\EvRoutes\Schemas;

use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Modules\Shared\Enums\VehicleOption;

class EvRouteForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Route')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                Select::make('ev_company_id')
                                    ->label('EV Company')
                                    ->relationship('company', 'name')
                                    ->required()
                                    ->searchable()
                                    ->preload(),
                                Select::make('from_destination_id')
                                    ->label('From')
                                    ->relationship('fromDestination', 'name')
                                    ->required()
                                    ->searchable()
                                    ->preload(),
                                Select::make('to_destination_id')
                                    ->label('To')
                                    ->relationship('toDestination', 'name')
                                    ->required()
                                    ->searchable()
                                    ->preload()
                                    ->different('from_destination_id')
                                    ->validationMessages([
                                        'different' => 'The destination must be different from the origin.',
                                    ]),
                                Select::make('timeSlots')
                                    ->label('Departure Time Slots')
                                    ->relationship('timeSlots', 'label')
                                    ->multiple()
                                    ->searchable()
                                    ->preload(),
                            ]),
                    ])
                    ->columnSpanFull(),
                Section::make('Options')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                Toggle::make('is_active')
                                    ->required()
                                    ->default(false)
                                    ->helperText('Every non-blocked vehicle option must have a price above 0 before a route can be activated.'),
                            ]),
                    ])
                    ->columnSpanFull(),
                Section::make('Pricing')
                    ->schema([
                        Repeater::make('pricing')
                            ->relationship()
                            ->hiddenLabel()
                            ->schema([
                                Select::make('vehicle_option')
                                    ->options(array_combine(
                                        array_map(fn (VehicleOption $option) => $option->value, VehicleOption::cases()),
                                        array_map(fn (VehicleOption $option) => str($option->value)->headline()->toString(), VehicleOption::cases()),
                                    ))
                                    ->disabled()
                                    ->dehydrated()
                                    ->required(),
                                TextInput::make('price')
                                    ->numeric()
                                    ->minValue(0)
                                    ->required(),
                                Toggle::make('is_blocked')
                                    ->label('Blocked')
                                    ->helperText('Hidden from booking regardless of price.'),
                            ])
                            ->columns(3)
                            ->default(
                                collect(VehicleOption::cases())
                                    ->map(fn (VehicleOption $option) => [
                                        'vehicle_option' => $option->value,
                                        'price' => 0,
                                        'is_blocked' => false,
                                    ])
                                    ->all()
                            )
                            ->addable(false)
                            ->deletable(false)
                            ->reorderable(false),
                    ])
                    ->columnSpanFull(),
            ]);
    }
}
