<?php

namespace App\Filament\Resources\Consignments\Schemas;

use App\Models\Consignment;
use App\Models\GlobalSetting;
use App\Models\Shipment;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

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

            // ── Shipment ──────────────────────────────────────────
            Section::make('Shipment')
                ->schema([
                    Select::make('shipment_id')
                        ->label('Shipment')
                        ->relationship('shipment', 'reference')
                        ->searchable()
                        ->preload()
                        ->required()
                        ->live()
                        ->getOptionLabelFromRecordUsing(fn (Shipment $record) =>
                            "[{$record->type}] {$record->reference} — {$record->origin} → {$record->destination}"
                        )
                        ->helperText('Select the shipment this consignee belongs to'),
                ]),

            // ── Client ────────────────────────────────────────────
            Section::make('Consignee Details')
                ->schema([
                    Grid::make(2)->schema([
                        TextInput::make('customer_name')
                            ->label('Full Name')
                            ->required(),

                        TextInput::make('phone')
                            ->label('Phone Number')
                            ->tel()
                            ->required(),

                        TextInput::make('destination')
                            ->label('Destination')
                            ->placeholder('e.g. NBI, MSA, NKR')
                            ->default('NBI')
                            ->required(),
                    ]),
                ]),

            // ── Cargo ─────────────────────────────────────────────
            Section::make('Cargo Details')
                ->schema([
                    Grid::make(2)->schema([

                        TextInput::make('flight_no')
                            ->label('Flight No.')
                            ->nullable()
                            ->placeholder('Air freight slot — e.g. F1, F2, F3'),

                        TextInput::make('house_waybill_no')
                            ->label('House Waybill No. (HAWB)')
                            ->nullable()
                            ->placeholder('Air freight — e.g. 2600000'),

                        TextInput::make('container_no')
                            ->label('Container No.')
                            ->nullable()
                            ->placeholder('Sea freight slot — e.g. CN1, CN2, CN3'),

                        Textarea::make('description')
                            ->label('Description of Goods')
                            ->rows(2)
                            ->nullable()
                            ->columnSpanFull(),

                        TextInput::make('pcs')
                            ->label('Pieces / Cartons (PCS/CTN)')
                            ->numeric()
                            ->default(0)
                            ->required(),

                        TextInput::make('actual_weight_kgs')
                            ->label('Weight (KGS)')
                            ->numeric()
                            ->nullable()
                            ->helperText('Air freight only'),

                        TextInput::make('cbm')
                            ->label('CBM (m³)')
                            ->numeric()
                            ->step(0.001)
                            ->nullable()
                            ->helperText('Sea freight only'),
                    ]),
                ]),

            // ── Charges ───────────────────────────────────────────
            Section::make('Charges')
                ->description('Amounts are auto-calculated from global rates. Override if needed.')
                ->schema([
                    Grid::make(3)->schema([

                        TextInput::make('extra_charge_usd')
                            ->label('Extra Charge ($)')
                            ->numeric()
                            ->default(0)
                            ->prefix('$')
                            ->live(onBlur: true)
                            ->helperText('Air: "EXTRA CHARGE $" | Sea: "REMARKS $"'),

                        TextInput::make('amount_usd')
                            ->label('Total (USD)')
                            ->numeric()
                            ->default(0)
                            ->prefix('$')
                            ->helperText('Auto-calculated for air freight'),

                        TextInput::make('amount_kes')
                            ->label('Total (KES)')
                            ->numeric()
                            ->default(0)
                            ->prefix('KES')
                            ->required()
                            ->helperText('Auto-calculated — override if needed'),
                    ]),

                    // Live rate reference for staff
                    Grid::make(3)->schema([
                        Placeholder::make('_rate_air')
                            ->label('Air Rate (global)')
                            ->content(fn () =>
                                '$ ' . number_format(GlobalSetting::airRatePerKg(), 2) . ' per KG'
                            ),

                        Placeholder::make('_rate_sea')
                            ->label('Sea Rate (global)')
                            ->content(fn () =>
                                'KES ' . number_format(GlobalSetting::seaRatePerCbm(), 0) . ' per CBM'
                            ),

                        Placeholder::make('_exchange')
                            ->label('Exchange Rate')
                            ->content(fn () =>
                                'KES ' . number_format(GlobalSetting::exchangeRate(), 2) . ' per $1'
                            ),
                    ]),
                ]),

            // ── Status ────────────────────────────────────────────
            Section::make('Status')
                ->schema([
                    Select::make('status')
                        ->options(Consignment::statusOptions())
                        ->default('in_transit')
                        ->required()
                        ->native(false)
                        ->helperText(
                            'in_transit = not yet arrived | ' .
                            'in_warehouse = arrived | ' .
                            'ready_for_pickup = payment confirmed | ' .
                            'delivered = collected'
                        ),
                ])
                ->collapsed(),
        ]);
    }
}