<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @generated   2025-04-05 20:20:17
 * @package     bplan-base/globals
 * @subpackage  LivewireComponents
 * @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\Livewire\User\Modals;


use BplanBase\Globals\Repositories\RoleRepository;
use BplanBase\Globals\Services\RoleUserService;
use BplanBase\Globals\Services\UserService;
use BplanBase\Globals\Traits\InteractsWithBanner;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Component;


/**
 * Livewire Component
 *
 * @version     1.1.0 / 2026-03-17
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Create extends Component
{


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


    use InteractsWithBanner;


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


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


    /**
     * @var
     */
    public string $email;


    /**
     * @var
     */
    public string $name;


    /**
     * @var
     */
    public ?Collection $roles;


    /**
     * @var
     */
    public array $selectedRoles;




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


    /**
     * @var
     */
    private static $_logPrefix = 'User/Modals/Create';


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


    /**
     *
     * @version     1.1.0 / 2026-03-17
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function addUser()
    {
        $this->validate([
            'name' => 'required',
            'email' => 'required|email:rfc,dns|unique:users,email',
            'selectedRoles' => 'required|min:1',
        ]);

        try {
            $UserService = new UserService();

            $newUser = $UserService->create([
                'name' => $this->name,
                'email' => $this->email,
            ]);
            $roleUserService = new RoleUserService();

            foreach ($this->selectedRoles as $roleIndex => $isRoleSelected) {
                if($isRoleSelected) {
                    $roleUserService->create([
                        'user_id' => $newUser->id,
                        'role_id' => $this->roles[$roleIndex]->id
                    ]);
                }
            }
            $User = auth()->user();

            \AppLog::info('User '.$this->name.' ['.$this->email.'] created by ' . $User->name . ' ['.$User->email.']', prefix: self::$_logPrefix);

            $this->dispatch('init-tippyjs');
            $this->dispatch('user-created');

        } catch (\Throwable $th) {
            \AppLog::error($th, prefix: self::$_logPrefix);

            $this->dangerBanner('Error when adding user. Check the logs for more information.');
        }
    } // addUser()


    /**
     *
     * @version     2.0.0 / 2026-03-17
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function boot()
    {
        //
    } // boot()


    /**
     *
     * @version     1.0.0 / 2025-04-05
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function mount()
    {
        $this->roles = RoleRepository::getAll()->keyBy('id');

    } // mount()


    /**
     *
     * @version     1.0.0 / 2025-04-05
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.user.modals.create');

    } // render()


    /**
     *
     * @return      array
     *
     * @version     1.0.0 / 2025-04-05
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function rules(): array
    {
        $rules = [];

        return $rules;

    } // rules()


    /**
     *
     * @param       [type] $isSelected
     *
     * @param       [type] $role
     *
     * @return      void
     *
     * @version     1.0.0 / 2025-04-05
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function updatedSelectedRoles($isSelected, $role)
    {
        if (!$isSelected) {
            unset($this->selectedRoles[$role]);
        }
    } // updatedSelectedRoles()


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


} // class Create extends Component {}
