<?php
/**
 * Update Action Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Actions
 * @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\Actions\User;


use BplanBase\Globals\Foundation\Actions\BaseAction;
use BplanBase\Globals\Models\User;
use BplanBase\Globals\Notifications\PasswordReset;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;


/**
 * Update Action Class
 *
 * @version     1.1.0 / 2026-03-16
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class ResetPassword extends BaseAction
{

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


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


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


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


    /**
     * @var     string
     */
    private string $_logPrefix = 'User/PasswordResetAction';


    /**
     * @var     string
     */
    protected string $_modelClass = User::class;


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


    /**
     *
     * @param       int|string|User $user
     *
     * @return      User
     *
     * @version     1.1.0 / 2026-03-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function execute(int|string|User $user): User
    {
        $Model = $this->_resolveModel($user);

        $randomPassword = Str::random(12);

        $Model->password = Hash::make($randomPassword);

        $Model->save();
        /*
        **  Notification an den User senden. */
        $Model->notify(new PasswordReset($randomPassword));

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

        if ($AuthUser !== null) {
            \AppLog::info('New Password sent for user '.$Model->name.' '.$Model->email.' by '.$AuthUser->name.' ('.$AuthUser->email.')', prefix: self::$_logPrefix);
        }
        return $Model;

    } // execute()


} // class ResetPassword extends BaseAction {}
