<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\Actions;

use apexl\Io\modules\user\entities\userEntity;
use Exception;

final readonly class UserCreateAction
{
    /**
     * @throws Exception
     */
    public function create(
        string $firstName,
        string $lastName,
        string $email,
        string $password,
        bool $active = true,
        array $roles = [],
        array $permissions = [],
    ): userEntity {
        $user = new userEntity();

        $user->first_name = $firstName;
        $user->last_name = $lastName;
        $user->email = $email;
        $user->password = $password;
        $user->active = $active;

        foreach ($roles as $role) {
            $user->addRole($role);
        }

        foreach ($permissions as $permission) {
            $user->addPermission($permission);
        }

        $user->store();

        return $user;
    }
}