<?php
/**
 * Create Action Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @version     STUB: 1.3.0
 * @generated   2026-03-07 22:58:06
 * @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\Models\User;
use BplanBase\Globals\Foundation\Actions\BaseAction;
use BplanBase\Globals\Models\UserRecord;


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


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


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


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


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


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


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


    /**
     *
     * @param       array|User $attributes
     *
     * @return      UserRecord
     *
     * @version     4.0.0 / 2026-03-17
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function execute(array|User $attributes): UserRecord
    {
        $Model = new UserRecord();

        if ($attributes instanceof User) {
            $User = $attributes;
            $attributes = [];
            $lastName = null;

            foreach (['.', ' '] as $separator) {
                $nameParts = explode($separator, $User['name'], 2);
                $firstName = $nameParts[0];

                if (count($nameParts) > 1) {
                    $lastName = $nameParts[1];

                    break;
                }
            }
            $attributes['id']         = $User->id;
            $attributes['uuid']       = $User->uuid;
            $attributes['email']      = $User->email;
            $attributes['first_name'] = ucfirst($firstName);
            $attributes['last_name']  = $lastName ?? ucfirst($lastName);
        }
        $Model->fill($attributes);

        $Model->save();

        return $Model;

    } // execute()


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


} // class Create extends BaseAction {}
