<?php

namespace App\Filament\Resources\ShippingMethods\Schemas;

use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\Placeholder;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Schema;

class ShippingMethodForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([

                // ── Main Info ─────────────────────────────────────────────────
                Section::make('Basic Information')
                    ->icon('heroicon-o-truck')
                    ->schema([
                        TextInput::make('name')
                            ->label('Method Name')
                            ->placeholder('e.g. Steadfast Courier, Pathao Courier')
                            ->required()
                            ->maxLength(255)
                            ->columnSpanFull(),

                        Select::make('zone')
                            ->label('Delivery Zone')
                            ->options([
                                'inside_dhaka'  => '🏙️ Inside Dhaka',
                                'outside_dhaka' => '🗺️ Outside Dhaka',
                            ])
                            ->default('inside_dhaka')
                            ->required()
                            ->native(false),

                        TextInput::make('estimated_days')
                            ->label('Estimated Delivery')
                            ->numeric()
                            ->integer()
                            ->minValue(1)
                            ->maxValue(30)
                            ->placeholder('e.g. 2')
                            ->suffix('days')
                            ->helperText('Optional — displayed to customer'),

                        Toggle::make('is_active')
                            ->label('Active — This method is available for orders')
                            ->default(true)
                            ->columnSpanFull(),
                    ])
                    ->columns(2),

                // ── Pricing Type ──────────────────────────────────────────────
                Section::make('Pricing Configuration')
                    ->icon('heroicon-o-currency-dollar')
                    ->description('Choose how shipping cost is calculated for this method.')
                    ->schema([
                        Select::make('pricing_type')
                            ->label('Pricing Type')
                            ->options([
                                'flat'         => '📦 Flat Rate — same cost for every order',
                                'weight_based' => '⚖️ Weight-Based — cost depends on order weight',
                            ])
                            ->default('flat')
                            ->required()
                            ->live()
                            ->native(false)
                            ->columnSpanFull(),

                        // ── Flat Rate ─────────────────────────────────────────
                        TextInput::make('cost')
                            ->label('Flat Shipping Cost')
                            ->numeric()
                            ->required()
                            ->default(80.00)
                            ->prefix('৳')
                            ->minValue(0)
                            ->step(0.01)
                            ->placeholder('0.00')
                            ->hintIcon('heroicon-m-question-mark-circle', tooltip: 'Fixed cost charged on every order.')
                            ->columnSpanFull()
                            ->visible(fn ($get) => ($get('pricing_type') ?? 'flat') === 'flat'),

                        // ── Weight-Based ──────────────────────────────────────
                        TextInput::make('base_cost')
                            ->label('Base Fee')
                            ->numeric()
                            ->required()
                            ->default(60.00)
                            ->prefix('৳')
                            ->minValue(0)
                            ->step(0.01)
                            ->placeholder('0.00')
                            ->hintIcon('heroicon-m-question-mark-circle', tooltip: 'Base charge before adding weight.')
                            ->visible(fn ($get) => $get('pricing_type') === 'weight_based'),

                        TextInput::make('per_kg_rate')
                            ->label('Rate per Kilogram')
                            ->numeric()
                            ->required()
                            ->default(20.00)
                            ->prefix('৳')
                            ->suffix('/ kg')
                            ->minValue(0)
                            ->step(0.01)
                            ->placeholder('0.00')
                            ->hintIcon('heroicon-m-question-mark-circle', tooltip: 'Rate multiplied by total kg.')
                            ->visible(fn ($get) => $get('pricing_type') === 'weight_based'),

                        TextInput::make('max_weight_g')
                            ->label('Maximum Weight Limit')
                            ->numeric()
                            ->minValue(0.001)
                            ->suffix('kg')
                            ->placeholder('No limit')
                            ->hintIcon('heroicon-m-question-mark-circle', tooltip: 'Maximum weight allowed for this shipping method (in kg).')
                            ->afterStateHydrated(function (TextInput $component, $state) {
                                if (blank($state)) return;
                                $component->state((float) $state / 1000);
                            })
                            ->dehydrateStateUsing(function ($state): ?int {
                                if (blank($state)) return null;
                                return (int) round((float) $state * 1000);
                            })
                            ->visible(fn ($get) => $get('pricing_type') === 'weight_based'),

                        Placeholder::make('weight_formula_hint')
                            ->label('')
                            ->content('💡 Formula: Cost = Base Fee + (Total Weight kg × Rate per kg)')
                            ->columnSpanFull()
                            ->visible(fn ($get) => $get('pricing_type') === 'weight_based'),
                    ])
                    ->columns(3),

            ])
            ->columns(1);
    }
}
