<?php

namespace App\Livewire;

use App\Modules\PhoneNumberVerification\Rules\PhoneNumberVerificationRule;
use App\Modules\TurkmenPassport\Repositories\TurkmenPassportRepository;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Schemas\Components\Fieldset;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Exceptions\Halt;
use Illuminate\Contracts\View\View;
use Joaopaulolndev\FilamentEditProfile\Concerns\HasSort;
use Livewire\Component;

class UserProfileFields extends Component implements HasForms
{
    use HasSort;
    use InteractsWithForms;

    /** @var array<string, mixed> */
    public ?array $data = [];

    protected static int $sort = 10;

    public function mount(): void
    {
        $this->form->fill(); // @phpstan-ignore-line
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(__('Profile Information'))
                    ->aside()
                    ->columns(6)
                    ->description(__('Fill your account profile information'))
                    ->schema([
                        TextInput::make('first_name')
                            ->label(__('Name'))
                            ->string()
                            ->maxLength(255)
                            ->default(user()->first_name)
                            ->columnSpan(2)
                            ->required(),

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

                        TextInput::make('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)
                            ->default(user()->getOption('born_at'))
                            ->columnSpan(2)
                            ->required()
                            ->beforeOrEqual('today'),

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

                        TextInput::make('email')
                            ->label(__('Email'))
                            ->email()
                            ->default(user()->email)
                            ->unique(ignoreRecord: false)
                            ->columnSpan(2),

                        Fieldset::make(__('Passport'))
                            ->columns([
                                'default' => 1,
                                'md' => 6,
                                'xl' => 6,
                            ])
                            ->columnSpan(6)
                            ->schema([
                                Select::make('passport_serie')
                                    ->label(__('Passport serie'))
                                    ->options(TurkmenPassportRepository::values())
                                    ->native(false)
                                    ->default(user()->getOption('passport_serie'))
                                    ->required()
                                    ->columnSpan(2),

                                TextInput::make('passport_id')
                                    ->label(__('Passport number'))
                                    ->default(user()->getOption('passport_id'))
                                    ->required()
                                    ->columnSpan(2)
                                    ->mask('999999'),

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

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

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

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

                                TextInput::make('real_address')
                                    ->label(__('Current home address'))
                                    ->columnSpan(3)
                                    ->maxLength(255)
                                    ->default(user()->getOption('real_address'))
                                    ->required(),
                            ]),
                    ]),
            ])
            ->statePath('data');
    }

    public function save(): void
    {
        try {
            /** @var array{first_name: string, last_name: string, patronic_name: null|string, born_at: string, phone: string, email: string, passport_serie: string, passport_id: string, passport_given_at: string, born_place: string, passport_given_by: string, passport_address: string, real_address: string} */
            $data = $this->form->getState(); // @phpstan-ignore-line

            user()->update([
                'first_name' => $data['first_name'],
                'last_name' => $data['last_name'],
                'phone' => $data['phone'],
                'email' => $data['email'],
                'options->patronic_name' => $data['patronic_name'],
                'options->born_at' => $data['born_at'],
                'options->passport_serie' => $data['passport_serie'],
                'options->passport_id' => $data['passport_id'],
                'options->passport_given_at' => $data['passport_given_at'],
                'options->born_place' => $data['born_place'],
                'options->passport_given_by' => $data['passport_given_by'],
                'options->passport_address' => $data['passport_address'],
                'options->real_address' => $data['real_address'],
                'must_fill_profile' => false,
            ]);

            Notification::make()
                ->success()
                ->title(__('Profile updated'))
                ->send();
        } catch (Halt $exception) {
            return;
        }
    }

    public function render(): View
    {
        return view('livewire.user-passport-fields');
    }
}
