<?php

namespace App\Filament\Clusters\VisaMasterPayments\Resources\VisaMasterPaymentOrders\Schemas;

use App\Modules\FilamentPermission\Repositories\FilamentPermissionRepository;
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 App\Modules\VisaMasterPaymentOrder\Models\VisaMasterPaymentOrder;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
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 Filament\Support\Icons\Heroicon;
use Illuminate\Support\Facades\Auth;

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

                Section::make()
                    ->columnSpan(4)
                    ->columns(4)
                    ->disabled(fn (string $context): bool => FilamentPermissionRepository::forClients())
                    ->components([
                        Select::make('status')
                            ->label(__('Status'))
                            ->options(OrderStatusRepository::statusValues())
                            ->default(OrderStatusRepository::defaultStatus())
                            ->native(false)
                            ->columnSpan(2),

                        Toggle::make('paid')
                            ->label(sprintf('%s (%s)', __('Paid'), __('This month')))
                            ->inline(false)
                            ->disabled(true)
                            ->onIcon(Heroicon::CheckCircle)
                            ->offIcon(Heroicon::XCircle)
                            ->onColor('success')
                            ->offColor('danger'),

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

                Wizard::make([
                    Step::make(__('Order type and bank'))
                        ->schema([
                            Fieldset::make(__('Order type'))
                                ->schema([
                                    Select::make('type')
                                        ->label(__('Type'))
                                        ->options(VisaMasterPaymentOrder::applicationTypes())
                                        ->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) {
                                            $query->orderByTranslation('name');

                                            $region = $get('region');
                                            if ($region) {
                                                $query->where('region', $region);
                                            }
                                        })
                                        ->required(),
                                ]),
                        ]),
                    Step::make(__('Payment sender data'))
                        ->columns(8)
                        ->schema([
                            TextInput::make('sender_full_name')
                                ->label(__('Name, Surname, Patronic name'))
                                ->columnSpan(4)
                                ->default(user()->fullName())
                                ->required(),

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

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

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

                            TextInput::make('sender_deposit_account')
                                ->label(__('Deposit account'))
                                ->columnSpan(4)
                                ->required(),

                            TextInput::make('address')
                                ->label(__('Address'))
                                ->columnSpan(4)
                                ->required(),

                            Section::make(__('Files'))
                                ->description('PNG, JPEG, PDF')
                                ->columnSpanFull()
                                ->columns(2)
                                ->schema([
                                    SpatieMediaLibraryFileUpload::make('sender_passport_local')
                                        ->collection('sender_passport_local')
                                        ->label(__('Ugradyja degişli Türkmenistanyň raýatynyň (içki milli) pasportynyň asyl görnüşi we göçürmesi'))
                                        ->required(),

                                    SpatieMediaLibraryFileUpload::make('sender_passport_international')
                                        ->collection('sender_passport_international')
                                        ->label(__('Ugradyja degişli Türkmenistandan çykmak we Türkmenistana girmek üçin pasportynyň asyl görnüşi we  göçürmesi')),

                                    SpatieMediaLibraryFileUpload::make('sender_travel_stamp_on_passport')
                                        ->collection('sender_travel_stamp_on_passport')
                                        ->label(__('Ugradyja degişli Türkmenistandan çykmak we Türkmenistana girmek üçin pasportyndaky daşary döwletine gidendigi we daşary  döwlete barandygy baradaky  (ştampyň) bellenen sahypasynyň göçürmesi'))
                                        ->required(),

                                    SpatieMediaLibraryFileUpload::make('sender_proof_of_kinship')
                                        ->collection('sender_proof_of_kinship')
                                        ->label(__('Ugradyjynyň we kabul edijiniň (talybyň) özara garyndaşlyk derejesini tassyklaýjy resminamalarynyň göçürmesi')),

                                    SpatieMediaLibraryFileUpload::make('sender_passport_local_old')
                                        ->collection('sender_passport_local_old')
                                        ->label(__('Ugradyjy we kabul ediji (talyp) 2015-nji ýyldan soňra Türkmenistanyň raýatynyň pasportyny ikinji gezek alan bolsa, onda birinji gezek alan pasportynyň seriýasy baradaky maglumat')),

                                    SpatieMediaLibraryFileUpload::make('sender_passport_local_old_replacement')
                                        ->collection('sender_passport_local_old_replacement')
                                        ->label(__('Ugradyjy we kabul ediji (talyp) 2015-nji ýyldan soňra Türkmenistanyň raýatynyň pasportyny ikinji gezek alandan soňra birinji gezek alan pasportynyň seriýasy baradaky maglumaty bilmeýän ,bolsa onda polisiýanyň degişli edaralaryndan birinji alan pasportynyň seriýasy baradaky güwänamasy')),
                                ]),
                        ]),

                    Step::make(__('Payee information'))
                        ->columns(8)
                        ->schema([
                            TextInput::make('reciever_full_name')
                                ->label(__('Name, Surname, Patronic name'))
                                ->columnSpan(5)
                                ->required(),

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

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

                            Section::make(__('Files'))
                                ->description('PNG, JPEG, PDF')
                                ->columnSpanFull()
                                ->columns(2)
                                ->schema([
                                    SpatieMediaLibraryFileUpload::make('receiver_requisite')
                                        ->collection('receiver_requisite')
                                        ->label(__('Talyba degişli walýuta "VISA" kartyň rekwizitleri'))
                                        ->maxSize(4096)
                                        ->columnSpan(1)
                                        ->required(),

                                    SpatieMediaLibraryFileUpload::make('receiver_document_stating_he_is_studying')
                                        ->collection('receiver_document_stating_he_is_studying')
                                        ->label(__('Talybyň daşary ýurt döwletiniň ýokary okuw mekdebinde okaýandygy baradaky güwänamasy'))
                                        ->maxSize(4096)
                                        ->openable()
                                        ->columnSpan(1),

                                    SpatieMediaLibraryFileUpload::make('receiver_ticket')
                                        ->collection('receiver_ticket')
                                        ->label(__('Talybyň bilediniň göçürmesi'))
                                        ->maxSize(4096)
                                        ->columnSpan(1),

                                    SpatieMediaLibraryFileUpload::make('receiver_passport_local')
                                        ->collection('receiver_passport_local')
                                        ->label(__('Talyba degişli Türkmenistanyň raýatynyň (içki milli) pasportynyň asyl görnüşi we göçürmesi'))
                                        ->maxSize(4096)
                                        ->columnSpan(1),

                                    SpatieMediaLibraryFileUpload::make('receiver_passport_international')
                                        ->collection('receiver_passport_international')
                                        ->label(__('Talybyň Türkmenistandan çykmak we Türkmenistana girmek üçin (zagran) pasportynyň göçürmesi'))
                                        ->maxSize(4096)
                                        ->columnSpan(1),

                                    SpatieMediaLibraryFileUpload::make('receiver_visa')
                                        ->collection('receiver_visa')
                                        ->label(__('Talybyň Türkmenistandan çykmak we Türkmenistana girmek üçin pasportyndaky daşary ýurtda galyp okap bilýändigi  baradaky berlen möhleti hereket edýän rugsatnamasynyň (wizasynyň) bellenen sahypasynyň göçürmesi'))
                                        ->maxSize(4096)
                                        ->columnSpan(1),

                                    SpatieMediaLibraryFileUpload::make('receiver_travel_stamp_on_passport')
                                        ->collection('receiver_travel_stamp_on_passport')
                                        ->label(__('Talybyň Türkmenistandan çykmak we Türkmenistana girmek üçin pasportyndaky Türkmenistandan çykandygy we daşary ýurt döwletine girendigi baradaky ştamplaryň (seneli ştampyň) bellenen sahypasynyň göçürmesi'))
                                        ->maxSize(4096)
                                        ->columnSpan(1),

                                    SpatieMediaLibraryFileUpload::make('receiver_document_stating_he_is_studying_2')
                                        ->collection('receiver_document_stating_he_is_studying_2')
                                        ->label(__('Talybyň daşary ýurt döwletiniň ýokary okuw mekdebinde okaýandygy baradaky güwänamasyndaky maglumatyň doly takyk däl ýagdaýynda takyk däl maglumatyň sebäpleri baradaky daşary ýurt döwletiniň ýokary okuw mekdebinden haty'))
                                        ->maxSize(4096)
                                        ->columnSpan(1),
                                ]),
                        ]),

                ])->columnSpanFull()->skippable(fn (string $context) => $context === 'edit'),
            ]);
    }
}
