<?php

namespace App\Filament\Resources\DelayedGoods\Schemas;

use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Schema;

class DelayedGoodForm
{
    public static function configure(Schema $schema): Schema
    {
        $recalculateTotal = function (callable $get, callable $set): void {
            $quantity = (int) str()->replace('.', '', (string) ($get('quantity') ?? 0));
            $unitPrice = (int) str()->replace('.', '', (string) ($get('unit_price') ?? 0));
            $totalAmount = $quantity * $unitPrice;
            $set('total_amount', number_format($totalAmount, 0, ',', '.'));
        };

        $recalculateRemaining = function (callable $get, callable $set): void {
            $paidAmount = (int) str()->replace('.', '', (string) ($get('paid_amount') ?? 0));
            $totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));

            $remaining = max(0, $totalAmount - $paidAmount);

            $set('remaining_amount', number_format($remaining, 0, ',', '.'));
        };

        return $schema->components([
            Grid::make(2)
                ->schema([
                    DatePicker::make('stored_date')
                        ->label('Tanggal Penyimpanan')
                        ->default(now())
                        ->required()
                        ->native(false)
                        ->displayFormat('l, d F Y'),

                    Select::make('customer_id')
                        ->label('Pelanggan')
                        ->relationship('customer', 'name')
                        ->searchable()
                        ->preload()
                        ->native(false)
                        ->required(),
                ]),

            Grid::make(3)
                ->schema([
                    TextInput::make('quantity')
                        ->label('Jumlah Barang')
                        ->placeholder('0')
                        ->autocomplete(false)
                        ->required()
                        ->live()
                        ->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal, $recalculateRemaining): void {
                            $recalculateTotal($get, $set);
                            $recalculateRemaining($get, $set);
                        })
                        ->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
                        ->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),

                    TextInput::make('unit_price')
                        ->label('Harga per Satuan')
                        ->placeholder('0')
                        ->autocomplete(false)
                        ->prefix('Rp')
                        ->required()
                        ->live()
                        ->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
                        ->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0)))
                        ->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateTotal, $recalculateRemaining): void {
                            $recalculateTotal($get, $set);
                            $recalculateRemaining($get, $set);
                        }),

                    Select::make('unit_id')
                        ->label('Satuan')
                        ->relationship('unit', 'alias')
                        ->searchable()
                        ->preload()
                        ->native(false)
                        ->required(),
                ]),

            Grid::make(3)
                ->schema([
                    TextInput::make('total_amount')
                        ->label('Total')
                        ->placeholder('0')
                        ->readOnly()
                        ->prefix('Rp')
                        ->required()
                        ->live()
                        ->formatStateUsing(
                            fn ($state) => number_format((int) ($state ?? 0), 0, ',', '.')
                        )
                        ->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),

                    TextInput::make('paid_amount')
                        ->label('Jumlah yang Sudah Dibayar (DP)')
                        ->placeholder('0')
                        ->default(0)
                        ->autocomplete(false)
                        ->required()
                        ->live()
                        ->prefix('Rp')
                        ->readOnly(fn (callable $get) => $get('is_paid'))
                        ->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateRemaining): void {
                            $paidAmount = (int) str()->replace('.', '', (string) ($state ?? 0));
                            $totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));

                            $recalculateRemaining($get, $set);

                            if (! $get('is_paid')) {
                                $set('is_paid', $paidAmount >= $totalAmount && $totalAmount > 0);
                            }

                            if ($paidAmount >= $totalAmount && $totalAmount > 0 && ! $get('payment_date')) {
                                $set('payment_date', now());
                            }

                            if ($paidAmount < $totalAmount) {
                                $set('payment_date', null);
                            }
                        })
                        ->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
                        ->dehydrateStateUsing(fn ($state) => (int) str()->replace('.', '', (string) ($state ?? 0))),

                    TextInput::make('remaining_amount')
                        ->label('Sisa Pembayaran')
                        ->placeholder('0')
                        ->readOnly()
                        ->prefix('Rp')
                        ->default(0)
                        ->live()
                        ->formatStateUsing(function ($state, callable $get) {
                            $paidAmount = (int) str()->replace('.', '', (string) ($get('paid_amount') ?? 0));
                            $totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));
                            $remaining = max(0, $totalAmount - $paidAmount);

                            return number_format($remaining, 0, ',', '.');
                        })
                        ->dehydrated(false),

                    Toggle::make('is_paid')
                        ->label('Lunas')
                        ->default(false)
                        ->live()
                        ->afterStateUpdated(function ($state, callable $get, callable $set) use ($recalculateRemaining): void {
                            $totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));

                            if ($state) {
                                $set('paid_amount', $totalAmount);

                                if (! $get('payment_date')) {
                                    $set('payment_date', now());
                                }
                            } else {
                                $set('paid_amount', 0);
                                $set('payment_date', null);
                            }

                            $recalculateRemaining($get, $set);
                        }),

                    DatePicker::make('payment_date')
                        ->label('Tanggal Pembayaran Lunas')
                        ->native(false)
                        ->displayFormat('l, d F Y')
                        ->visible(function (callable $get) {
                            $paidAmount = (int) str()->replace('.', '', (string) ($get('paid_amount') ?? 0));
                            $totalAmount = (int) str()->replace('.', '', (string) ($get('total_amount') ?? 0));

                            return $paidAmount >= $totalAmount && $totalAmount > 0;
                        }),
                ]),

            Textarea::make('notes')
                ->label('Catatan')
                ->placeholder('...')
                ->autocomplete(false)
                ->columnSpanFull(),
        ])
            ->columns(1);
    }
}
