<?php

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

use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Illuminate\Validation\Rules\Unique;

class PopularRouteForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Popular Route')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                Select::make('from_destination_id')
                                    ->label('From')
                                    ->relationship('fromDestination', 'name')
                                    ->required()
                                    ->searchable()
                                    ->preload()
                                    ->live(),
                                Select::make('to_destination_id')
                                    ->label('To')
                                    ->relationship('toDestination', 'name')
                                    ->required()
                                    ->searchable()
                                    ->preload()
                                    ->different('from_destination_id')
                                    ->unique(
                                        modifyRuleUsing: fn (Unique $rule, Get $get) => $rule->where('from_destination_id', $get('from_destination_id')),
                                        ignoreRecord: true,
                                    )
                                    ->validationMessages([
                                        'different' => 'The destination must be different from the origin.',
                                        'unique' => 'A popular route between these destinations already exists.',
                                    ]),
                            ]),
                        Textarea::make('description')
                            ->columnSpanFull(),
                        Textarea::make('mm_description')
                            ->label('Myanmar Description')
                            ->columnSpanFull(),
                        Toggle::make('is_active')
                            ->required()
                            ->default(true),
                    ]),
            ]);
    }
}
