<?php

namespace App\Filament\Clusters\Cards\CardOrders\Schemas;

use App\Modules\Country\Repositories\CountryRepository;
use App\Modules\OrderStatus\Repositories\OrderStatusRepository;
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\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
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 CardOrderForm
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
                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),

                        Toggle::make('paid')
                            ->label(__('Paid'))
                            ->inline(false)
                            ->required(),

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

                Wizard::make([
                    Step::make(__('Loan & Bank'))
                        ->schema([
                            Fieldset::make(__('Loan type and amount'))
                                ->schema([
                                    Select::make('card_state_id')
                                        ->relationship('cardState', 'name')
                                        ->label(__('Card state'))
                                        ->native(false)
                                        ->required(),

                                    Select::make('card_type_id')
                                        ->relationship('cardType', 'name')
                                        ->label(__('Card type'))
                                        ->native(false)
                                        ->required(),
                                ]),

                            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)
                                ->default(user()->first_name)
                                ->required()
                                ->maxLength(255)
                                ->autocomplete(Str::random(10)),

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

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

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

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

                                TextInput::make('passport_id')
                                    ->label(__('Passport number'))
                                    ->required()
                                    ->columnSpan(1)
                                    ->default(user()->getOption('passport_id'))
                                    ->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()
                                ->default(user()->getOption('passport_given_at'))
                                ->beforeOrEqual('today')
                                ->required(),

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

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

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

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

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

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

                            TextInput::make('job_location')
                                ->label(__('Work location and your position'))
                                ->columnSpan(6)
                                ->required(),

                            Select::make('citizenship')
                                ->label(__('Citizenship'))
                                ->options(CountryRepository::values())
                                ->default('TM')
                                ->columnSpan(2)
                                ->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),
                ])->columnSpanFull(),

                // Textarea::make('passport_one')
                //     ->required()
                //     ->columnSpanFull(),
                // Textarea::make('passport_two')
                //     ->required()
                //     ->columnSpanFull(),
                // Textarea::make('passport_three')
                //     ->required()
                //     ->columnSpanFull(),
                // Textarea::make('passport_four')
                //     ->required()
                //     ->columnSpanFull(),

            ]);
    }
}
