<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  LivewireComponents
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Livewire\User;


use BplanBase\Globals\Traits\InteractsWithBanner;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\ValidationException;
use Livewire\Component;


/**
 * Livewire Component
 *
 * @version     1.0.0 / 2026-04-26
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class UpdatePassword extends Component
{


/* +++ TRAITS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


    use InteractsWithBanner;


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * @var     string
     */
    public string $current_password = '';


    /**
     * @var     string
     */
    public string $password = '';


    /**
     * @var     string
     */
    public string $password_confirmation = '';


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * @return      void
     *
     * @version     1.0.0 / 2026-04-26
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render(): \Illuminate\View\View
    {
        return view('globals::livewire.user.update-password');

    } // render()


    /**
     * @return      void
     *
     * @version     1.0.0 / 2026-04-26
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function updatePassword(): void
    {
        try {
            $this->validate([
                'current_password'      => ['required', 'current_password'],
                'password'              => ['required', 'confirmed', Password::defaults()],
                'password_confirmation' => ['required'],
            ]);
        } catch (ValidationException $E) {
            $this->reset('current_password', 'password', 'password_confirmation');

            throw $E;
        }

        Auth::user()->update([
            'password' => Hash::make($this->password),
        ]);

        $this->reset('current_password', 'password', 'password_confirmation');

        $this->banner(__('globals::users.info.password-updated'));

    } // updatePassword()


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


} // class UpdatePassword extends Component {}
