<?php

namespace App\Filament\Clusters\Loans\LoanOrders\Schemas;

use App\Modules\LoanOrder\Models\LoanOrderRequiredDocs;
use App\Modules\LoanOrder\Repositories\LoanOrderRepository;
use App\Modules\OrderStatus\Repositories\OrderStatusRepository;
use App\Modules\PersonStates\Repositories\EducationRepository;
use App\Modules\PersonStates\Repositories\MarriageRepository;
use App\Modules\PhoneNumberVerification\Rules\PhoneNumberVerificationRule;
use App\Modules\Region\Repositories\RegionRepository;
use App\Modules\TurkmenPassport\Repositories\TurkmenPassportRepository;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Fieldset;
use Filament\Schemas\Components\FusedGroup;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Wizard;
use Filament\Schemas\Components\Wizard\Step;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class LoanOrderForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->columns(4)
            ->components([
                Hidden::make('source')->default('web'),
                Hidden::make('user_id')->default(Auth::id()),

                Section::make(__('New loan order'))
                    ->columnSpan(4)
                    ->columns(4)
                    ->components([
                        Select::make('status')
                            ->label(__('Status'))
                            ->options(OrderStatusRepository::statusValues())
                            ->default(OrderStatusRepository::defaultStatus())
                            ->native(false)
                            ->required()
                            ->columnSpan(2),

                        Select::make('satisfiable')
                            ->label(__('Loan history'))
                            ->options(LoanOrderRepository::satisfiableValues())
                            ->native(false)
                            ->columnSpan(2),

                        Select::make('loan_order_required_doc_id')
                            ->label(__('Required documents'))
                            ->relationship('requiredDocs', 'name')
                            ->searchable()
                            ->native(false)
                            ->preload()
                            ->live()
                            ->afterStateUpdated(function ($state, callable $set) {
                                if ($state) {
                                    /** @var null|LoanOrderRequiredDocs */
                                    $requiredDoc = LoanOrderRequiredDocs::find($state);

                                    if ($requiredDoc) {
                                        $set('notes', $requiredDoc->value);
                                    }
                                }
                            })
                            ->columnSpanFull(),

                        RichEditor::make('notes')
                            ->label(__('Bellik'))
                            ->columnSpanFull(),
                    ]),

                Wizard::make([
                    Step::make(__('Loan & Bank'))
                        ->schema([
                            Fieldset::make(__('Loan type and amount'))
                                ->schema([
                                    Select::make('loan_type')
                                        ->label(__('Loan type'))
                                        ->relationship('loanType', 'name')
                                        ->required(),

                                    TextInput::make('loan_amount')
                                        ->label(__('Loan amount'))
                                        ->numeric()
                                        ->required()
                                        ->minValue(1)
                                        ->maxValue(40000)
                                        ->suffix('TMT')
                                        ->belowContent(__('Max is 40 000 TMT')),
                                ]),

                            Fieldset::make(__('Location'))
                                ->schema([
                                    Select::make('region')
                                        ->label(__('Region'))
                                        ->options(RegionRepository::values())
                                        ->live()
                                        ->afterStateUpdated(fn (callable $set) => $set('branch_id', null))
                                        ->required(),

                                    Select::make('branch_id')
                                        ->label(__('Branch'))
                                        ->relationship('branch', 'name', function ($query, callable $get) {
                                            $region = $get('region');
                                            if ($region) {
                                                $query->where('region', $region);
                                            }
                                        })
                                        ->required(),
                                ]),
                        ]),
                    Step::make(__('Personal information'))
                        ->columns(8)
                        ->schema([
                            TextInput::make('customer_name')
                                ->label(__('Name'))
                                ->columnSpan(2)
                                ->required()
                                ->maxLength(255)
                                ->autocomplete(Str::random(10)),

                            TextInput::make('customer_surname')
                                ->label(__('Surname'))
                                ->columnSpan(2)
                                ->required()
                                ->maxLength(255),

                            TextInput::make('customer_patronic_name')
                                ->label(__('Patronic name'))
                                ->columnSpan(2)
                                ->maxLength(255),

                            DatePicker::make('born_at')
                                ->displayFormat('d.m.Y')
                                ->label(__('Birth date'))
                                ->native(false)
                                ->columnSpan(2)
                                ->required()
                                ->beforeOrEqual('today'),

                            FusedGroup::make([
                                Select::make('passport_serie')
                                    ->label(__('Passport serie'))
                                    ->options(TurkmenPassportRepository::values())
                                    ->native(false)
                                    ->required()
                                    ->columnSpan(1),

                                TextInput::make('passport_id')
                                    ->label(__('Passport number'))
                                    ->required()
                                    ->columnSpan(1)
                                    ->mask('999999'),
                            ])
                                ->columnSpan(3)
                                ->label(__('Passport serie and number'))
                                ->columns(2),

                            DatePicker::make('passport_given_at')
                                ->label(__('Passport given date'))
                                ->columnSpan(2)
                                ->displayFormat('d.m.Y')
                                ->native(false)
                                ->closeOnDateSelection()
                                ->beforeOrEqual('today')
                                ->required(),

                            TextInput::make('born_place')
                                ->columnSpan(3)
                                ->label(__('Born place (passport)'))
                                ->maxLength(255)
                                ->required(),

                            TextInput::make('passport_given_by')
                                ->label(__('Passport given by'))
                                ->columnSpan(4)
                                ->maxLength(255)
                                ->required(),

                            TextInput::make('passport_address')
                                ->columnSpan(4)
                                ->label(__('Proscription for home'))
                                ->maxLength(255)
                                ->required(),

                            TextInput::make('real_address')
                                ->label(__('Current home address'))
                                ->columnSpan(4)
                                ->maxLength(255)
                                ->required(),

                            TextInput::make('email')
                                ->label(__('Email'))
                                ->email()
                                ->maxLength(255)
                                ->columnSpan(2),

                            TextInput::make('phone')
                                ->label(__('Phone'))
                                ->required()
                                ->mask('99 99 99 99')
                                ->prefix('+993')
                                ->rules([
                                    new PhoneNumberVerificationRule,
                                ])
                                ->columnSpan(2),

                            TextInput::make('phone_additional')
                                ->label(__('Additional phone'))
                                ->mask('99 99 99 99')
                                ->prefix('+993')
                                ->rules([
                                    new PhoneNumberVerificationRule,
                                ])
                                ->columnSpan(2),

                            TextInput::make('phone_home')
                                ->label(__('Home phone'))
                                ->numeric()
                                ->prefix('+993')
                                ->columnSpan(2),

                            Select::make('education')
                                ->columnSpan(2)
                                ->label(__('Education'))
                                ->options(EducationRepository::values())
                                ->native(false)
                                ->required(),

                            Select::make('marriage_status')
                                ->columnSpan(2)
                                ->label(__('Marital status'))
                                ->options(MarriageRepository::values())
                                ->native(false)
                                ->required(),
                        ]),
                    Step::make(__('Pasport files'))
                        ->columns(4)
                        ->schema([
                            FileUpload::make('passport_one')
                                ->label(__('Passport (page 1)'))
                                ->image()
                                ->maxSize(4096)
                                ->required()
                                ->columnSpan(2),

                            FileUpload::make('passport_two')
                                ->label(__('Passport (page 2-3)'))
                                ->image()
                                ->maxSize(4096)
                                ->required()
                                ->columnSpan(2),

                            FileUpload::make('passport_three')
                                ->label(__('Passport (page 8-9)'))
                                ->image()
                                ->maxSize(4096)
                                ->required()
                                ->columnSpan(2),

                            FileUpload::make('passport_four')
                                ->label(__('Passport (page 32)'))
                                ->image()
                                ->maxSize(4096)
                                ->required()
                                ->columnSpan(2),
                        ])->columnSpan(4),
                    Step::make(__('Work'))
                        ->columns(4)
                        ->schema([
                            Select::make('work_region')
                                ->label(__('Work region'))
                                ->options(RegionRepository::values())
                                ->columnSpan(1)
                                ->live()
                                ->afterStateUpdated(fn (callable $set) => $set('branch_id', null))
                                ->required(),

                            Select::make('work_province_id')
                                ->label(__('Work province'))
                                ->relationship('workProvince', 'name', function ($query, callable $get) {
                                    $region = $get('work_region');
                                    if ($region) {
                                        $query->where('region', $region);
                                    }
                                })
                                ->columnSpan(1)
                                ->required(),

                            TextInput::make('work_company')
                                ->label(__('Work company name'))
                                ->maxLength(255)
                                ->required()
                                ->columnSpan(2),

                            TextInput::make('work_company_accountant_number')
                                ->label(__('HR number'))
                                ->prefix('+993')
                                ->numeric()
                                ->required()
                                ->columnSpan(1),

                            TextInput::make('work_position')
                                ->label(__('Work position'))
                                ->required()
                                ->maxLength(255)
                                ->columnSpan(1),

                            TextInput::make('work_salary')
                                ->label(__('Salary'))
                                ->numeric()
                                ->required()
                                ->columnSpan(1),

                            DatePicker::make('work_started_at')
                                ->label(__('Work started at'))
                                ->displayFormat('d.m.Y')
                                ->beforeOrEqual('today')
                                ->required()
                                ->columnSpan(1),
                        ]),
                ])->columnSpan(4),
            ]);
    }
}
