<?php

namespace App\Filament\Pages;

use BackedEnum;
use UnitEnum;
use Filament\Forms;
use Filament\Pages\Page;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Facades\Hash;

class MyProfile extends Page
{
    protected static string|BackedEnum|null $navigationIcon = Heroicon::UserCircle;

    protected static bool $shouldRegisterNavigation = false;

    protected static string|UnitEnum|null $navigationGroup = 'administration';
    
    protected static ?int $navigationSort = 99;

    protected string $view = 'filament.pages.my-profile';

    public ?array $data = [];

    public static function getNavigationLabel(): string
    {
        return __('filament/admin/my_profile.navigation_label');
    }

    public static function getLabel(): string
    {
        return __('filament/admin/my_profile.navigation_label');
    }

    public function getTitle(): string
    {
        return __('filament/admin/my_profile.title');
    }

    public function mount(): void
    {
        $this->form->fill([
            'name' => auth()->user()->name,
            'email' => auth()->user()->email,
        ]);
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Section::make(__('Profile Information'))
                    ->description(__('Update your account profile information and email address.'))
                    ->schema([
                        TextInput::make('name')
                            ->label(__('Name'))
                            ->required()
                            ->maxLength(255),
                        
                        TextInput::make('email')
                            ->label(__('Email'))
                            ->email()
                            ->required()
                            ->maxLength(255),
                    ])
                    ->columns(2),

                Section::make(__('Update Password'))
                    ->description(__('Ensure your account is using a long, random password to stay secure.'))
                    ->schema([
                        TextInput::make('current_password')
                            ->label(__('Current Password'))
                            ->password()
                            ->revealable()
                            ->nullable()
                            ->dehydrated(false)
                            ->helperText(__('Leave blank to keep current password')),
                        
                        TextInput::make('password')
                            ->label(__('New Password'))
                            ->password()
                            ->revealable()
                            ->nullable()
                            ->minLength(8)
                            ->same('password_confirmation')
                            ->dehydrated(fn ($state) => filled($state)),
                        
                        TextInput::make('password_confirmation')
                            ->label(__('Confirm Password'))
                            ->password()
                            ->revealable()
                            ->nullable()
                            ->minLength(8)
                            ->dehydrated(false),
                    ])
                    ->columns(2),
            ])
            ->statePath('data');
    }

    public function updateProfile(): void
    {
        $data = $this->form->getState();

        $user = auth()->user();

        // Validate current password if new password provided
        if (filled($data['current_password'])) {
            if (!Hash::check($data['current_password'], $user->password)) {
                Notification::make()
                    ->title(__('Current password is incorrect'))
                    ->danger()
                    ->send();
                return;
            }
        }

        // Update basic info
        $user->name = $data['name'];
        $user->email = $data['email'];

        // Update password if provided
        if (filled($data['password'])) {
            $user->password = Hash::make($data['password']);
        }

        $user->save();

        Notification::make()
            ->title(__('Profile updated successfully'))
            ->success()
            ->send();
    }
}
