<?php

namespace App\Filament\Clusters\Cards\Resources\CardPinOrders\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 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\FusedGroup;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

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

                Section::make(__('New card pin order'))
                    ->columnSpanFull()
                    ->disabled(fn (string $context): bool => FilamentPermissionRepository::forClients())
                    ->hidden(fn (string $context) => FilamentPermissionRepository::defaultSystemInput($context))
                    ->components([
                        Select::make('status')
                            ->label(__('Status'))
                            ->options(OrderStatusRepository::statusValues())
                            ->default(OrderStatusRepository::defaultStatus())
                            ->native(false)
                            ->required()
                            ->columnSpanFull(),

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

                Section::make(__('New card pin order'))
                    ->columnSpanFull()
                    ->columns(6)
                    ->components([
                        Select::make('card_type_id')
                            ->relationship('cardType', 'name', fn (Builder $query) => $query->orderByTranslation('name'))
                            ->label(__('Card type'))
                            ->native(false)
                            ->columnSpan(3)
                            ->required(),

                        TextInput::make('card_number')
                            ->label(__('Card number'))
                            ->mask('9999 9999 9999 9999')
                            ->maxLength(255)
                            ->columnSpan(3)
                            ->required(),

                        Select::make('region')
                            ->label(__('Region'))
                            ->options(RegionRepository::values())
                            ->live()
                            ->afterStateUpdated(fn (callable $set) => $set('branch_id', null))
                            ->columnSpan(3)
                            ->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);
                                }
                            })
                            ->columnSpan(3)
                            ->required(),

                        TextInput::make('customer_name')
                            ->label(__('Name'))
                            ->columnSpan(2)
                            ->default(user()->first_name)
                            ->required()
                            ->maxLength(255)
                            ->autocomplete(Str::random(10))
                            ->columnSpan(2),

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

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

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

                        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(2)
                            ->label(__('Passport serie and number'))
                            ->columns(2),

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

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

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

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

                        FileUpload::make('passport_four')
                            ->label(__('Passport (page 32)'))
                            ->image()
                            ->maxSize(4096)
                            ->required()
                            ->columnSpan(3),
                    ]),

            ]);
    }
}
