<?php

namespace App\Filament\Pages\Auth;

use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\FileUpload;
use Filament\Schemas\Schema;
use Filament\Auth\Pages\EditProfile as BaseEditProfile;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Utilities\Get;

class EditProfile extends BaseEditProfile
{
    protected static string $layout = 'filament-panels::components.layout.index';

    protected string $view = 'filament-panels::pages.page';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Grid::make(1)
                    ->schema([
                        Section::make('Personal Details')
                            ->description('Update account details.')
                            ->schema([
                                Grid::make(4)
                                    ->schema([
                                        FileUpload::make('avatar')
                                            ->label('Avatar')
                                            ->image()
                                            ->avatar()
                                            ->disk(config('filesystems.default'))
                                            ->directory('avatars')
                                            ->imagePreviewHeight('80')
                                            ->columnSpan(1),
                                        Grid::make(2)
                                            ->schema([
                                                $this->getNameFormComponent()
                                                    ->columnSpan(1),
                                                $this->getEmailFormComponent()
                                                    ->columnSpan(1),
                                                TextInput::make('phone')
                                                    ->label('Phone Number')
                                                    ->tel()
                                                    ->unique('users', 'phone', ignoreRecord: true)
                                                    ->columnSpan(1),
                                                DatePicker::make('birthday')
                                                    ->label('Birthday')
                                                    ->maxDate(now())
                                                    ->columnSpan(1),
                                            ])
                                            ->columnSpan(3),
                                    ]),
                            ]),

                        Section::make('Update Password')
                            ->description('Change account password.')
                            ->schema([
                                $this->getCurrentPasswordFormComponent()
                                    ->visible(true)
                                    ->required(fn (Get $get): bool => filled($get('password')) || ($get('email') !== $this->getUser()->getAttributeValue('email')))
                                    ->columnSpan(2),
                                $this->getPasswordFormComponent()
                                    ->columnSpan(1),
                                $this->getPasswordConfirmationFormComponent()
                                    ->visible(true)
                                    ->required(fn (Get $get): bool => filled($get('password')))
                                    ->columnSpan(1),
                            ])
                            ->columns(2),
                    ]),
            ]);
    }
}
