<?php
/**
 * Update Action Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @version     STUB: 1.3.0
 * @generated   2025-06-18 13:14:15
 * @package     bplan-base/globals
 * @subpackage  Actions
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @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\UserRecord;


use BplanBase\Globals\Actions\User\UpdateEmail as UpdateUserEmail;
use BplanBase\Globals\Foundation\Actions\BaseAction;
use BplanBase\Globals\Models\UserRecord;


/**
 * Update Action Class
 *
 * @version     1.5.0 / 2026-03-16
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Update extends BaseAction
{


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


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


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


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


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


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


    /**
     *
     * @param       int|string|UserRecord $userRecord
     *
     * @param       array $attributes
     *
     * @return      UserRecord
     *
     * @version     3.3.0 / 2026-03-16
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function execute(int|string|UserRecord $userRecord, array $attributes): UserRecord
    {
        $Model = $this->_resolveModel($userRecord);

        $originalEmail = $Model->email;

        $Model->fill($attributes);

        $Model->save();

        if (isset($attributes['email']) && $attributes['email'] !== $originalEmail) {
            $Action = new UpdateUserEmail();

            $Action->execute($Model->id, $attributes['email']);
        }
        return $Model;

    } // execute()


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


} // class Update extends BaseAction {}
