<?php

namespace App\Filament\Resources\Users\Pages;

use App\Filament\Resources\Users\UserResource;
use Filament\Actions\DeleteAction;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;

class EditUser extends EditRecord implements Forms\Contracts\HasForms
{
    use Forms\Concerns\InteractsWithForms;

    protected static string $resource = UserResource::class;

    protected string $view = 'filament.resources.users.pages.edit-user';

    public ?array $profileData = [];
    public ?array $passwordData = [];

    public function mount(int | string $record): void
    {
        parent::mount($record);
        
        $this->fillForms();
    }

    protected function fillForms(): void
    {
        $this->profileData = [
            'name' => $this->record->name,
            'email' => $this->record->email,
        ];
        
        $this->passwordData = [
            'current_password' => '',
            'password' => '',
            'password_confirmation' => '',
        ];
    }

    public function profileForm(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Profile Information')
                    ->description('Update the user\'s profile information and email address.')
                    ->icon('heroicon-o-user')
                    ->schema([
                        Forms\Components\TextInput::make('name')
                            ->label('Name')
                            ->required()
                            ->maxLength(255)
                            ->prefixIcon('heroicon-o-user')
                            ->placeholder('Enter full name'),
                        
                        Forms\Components\TextInput::make('email')
                            ->label('Email')
                            ->email()
                            ->required()
                            ->maxLength(255)
                            ->prefixIcon('heroicon-o-envelope')
                            ->placeholder('Enter email address')
                            ->unique(table: 'users', column: 'email', ignoreRecord: $this->record->id),
                    ])
                    ->columns(1),
            ])
            ->statePath('profileData')
            ->model($this->record);
    }

    public function passwordForm(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Update Password')
                    ->description('Ensure the account is using a long, random password to stay secure.')
                    ->icon('heroicon-o-lock-closed')
                    ->schema([
                        Forms\Components\TextInput::make('current_password')
                            ->label('Current Password')
                            ->password()
                            ->required()
                            ->revealable()
                            ->prefixIcon('heroicon-o-key')
                            ->placeholder('Enter current password')
                            ->rule('current_password'),
                        
                        Forms\Components\TextInput::make('password')
                            ->label('New Password')
                            ->password()
                            ->required()
                            ->rule(Password::default())
                            ->revealable()
                            ->confirmed()
                            ->prefixIcon('heroicon-o-lock-closed')
                            ->placeholder('Enter new password')
                            ->helperText('Must be at least 8 characters')
                            ->validationAttribute('new password'),
                        
                        Forms\Components\TextInput::make('password_confirmation')
                            ->label('Confirm Password')
                            ->password()
                            ->required()
                            ->revealable()
                            ->prefixIcon('heroicon-o-lock-closed')
                            ->placeholder('Confirm new password')
                            ->dehydrated(false),
                    ])
                    ->columns(1),
            ])
            ->statePath('passwordData');
    }

    public function updateProfile(): void
    {
        $data = $this->profileForm->getState();
        
        $this->record->update([
            'name' => $data['name'],
            'email' => $data['email'],
        ]);

        Notification::make()
            ->success()
            ->title('Profile updated')
            ->body('The profile information has been updated successfully.')
            ->send();
    }

    public function updatePassword(): void
    {
        $data = $this->passwordForm->getState();
        
        $this->record->update([
            'password' => Hash::make($data['password']),
        ]);

        $this->passwordData = [
            'current_password' => '',
            'password' => '',
            'password_confirmation' => '',
        ];

        Notification::make()
            ->success()
            ->title('Password updated')
            ->body('The password has been updated successfully.')
            ->send();
    }

    protected function getForms(): array
    {
        return [
            'profileForm',
            'passwordForm',
        ];
    }

    protected function getHeaderActions(): array
    {
        return [
            DeleteAction::make(),
        ];
    }
}
