<?php
/**
 * Livewire Component
 *
 * @version     1.0.0
 * @package     bplan-base/globals
 * @subpackage  LivewireComponents
 * @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\Core\Role\Modals;


use BplanBase\Globals\Services\RoleService;
use BplanBase\Globals\Traits\InteractsWithBanner;
use Livewire\Attributes\On;
use Livewire\Attributes\Validate;
use Livewire\Component;


/**
 * Livewire Component
 *
 * @version     1.0.0 / 2026-06-07
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Create extends Component
{


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


    use InteractsWithBanner;


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


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


    /**
     * @var     bool
     */
    public bool $active = true;


    /**
     * @var     array|null
     */
    #[Validate('required')]
    public ?array $labelsJson = null;


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


    /**
     * @var     string
     */
    #[Validate('required|string|max:255')]
    public string $name = '';


    /**
     * @var     RoleService
     */
    protected RoleService $RoleService;


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


    /**
     * @var     string
     */
    private static string $_logPrefix = 'Core/Role/Modals/Create';


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


    /**
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function boot(RoleService $RoleService)
    {
        $this->RoleService = $RoleService;

        $this->locale = app()->getLocale();

    } // boot()


    /**
     *
     * @param       array|null $json
     *
     * @param       string $target
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    #[On('multi-language-json-created')]
    public function onMultiLanguageJsonCreated(?array $json, string $target): void
    {
        if ($target === 'create') {
            $this->labelsJson = $json;
        }

    } // onMultiLanguageJsonCreated()


    /**
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function createRole()
    {
        $this->validate();

        try {
            $Role = $this->RoleService->create([
                'active'     => $this->active,
                'guard_name' => 'web',
                'labels'     => $this->labelsJson,
                'name'       => $this->name,
            ]);

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

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

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

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

            $this->dangerBanner('Error when creating role. Check the logs for more information.');
        }

    } // createRole()


    /**
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.core.role.modals.create');

    } // render()


    /**
     *
     * @version     1.0.0 / 2026-06-07
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function resetModal()
    {
        $this->active     = true;
        $this->labelsJson = null;
        $this->name       = '';

    } // resetModal()


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


} // class Create extends Component {}
