<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @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\Core\Module\Modals;


use BplanBase\Globals\Http\Requests\UpdateModuleRequest;
use BplanBase\Globals\Models\Module;
use BplanBase\Globals\Services\ModuleService;
use BplanBase\Globals\Traits\InteractsWithBanner;
use Livewire\Attributes\On;
use Livewire\Component;


/**
 * Livewire Component
 *
 * @version     1.0.0 / 2026-05-25
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class Edit extends Component
{


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


    use InteractsWithBanner;


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


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


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


    /**
     * @var     string|null
     */
    public ?string $baseNamespace = null;


    /**
     * @var     string|null
     */
    public ?string $label = null;


    /**
     * @var     Module|null
     */
    public ?Module $_Module = null;


    /**
     * @var     string|null
     */
    public ?string $packageName = null;


    /**
     * @var     string|null
     */
    public ?string $routePrefix = null;


    /**
     * @var     ModuleService
     */
    protected ModuleService $_Service;


    /**
     * @var     string|null
     */
    public ?string $vendorName = null;


    /**
     * @var     string|null
     */
    public ?string $vendorPackage = null;


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


    /**
     * @var     string
     */
    private static string $_logPrefix = 'Core/Module/Modals/Edit';


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


    /**
     *
     * @param       ModuleService $Service
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function boot(ModuleService $Service): void
    {
        $this->_Service = $Service;

    } // boot()


    /**
     *
     * @param       array $module
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    #[On('show-edit-module-modal')]
    public function initModule(array $module): void
    {
        $this->_Module = Module::find($module['id']);

        $this->active        = (bool) $this->_Module->active;
        $this->baseNamespace = $this->_Module->base_namespace;
        $this->label         = $this->_Module->label;
        $this->packageName   = $this->_Module->package_name;
        $this->routePrefix   = $this->_Module->route_prefix;
        $this->vendorName    = $this->_Module->vendor_name;
        $this->vendorPackage = $this->_Module->vendor_package;

        $this->dispatch('module-modal-initialized');
        $this->dispatch('init-tippyjs');

    } // initModule()


    /**
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function mount(): void
    {
        //

    } // mount()


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

    } // render()


    /**
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function resetModal(): void
    {
        $this->active        = false;
        $this->baseNamespace = null;
        $this->label         = null;
        $this->packageName   = null;
        $this->routePrefix   = null;
        $this->vendorName    = null;
        $this->vendorPackage = null;
        $this->_Module       = null;

        $this->resetValidation();

    } // resetModal()


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

        $rules = UpdateModuleRequest::getRules($this->_Module->id);

        $rules['baseNamespace'] = $rules['base_namespace'];
        $rules['packageName']   = $rules['package_name'];
        $rules['routePrefix']   = $rules['route_prefix'];
        $rules['vendorName']    = $rules['vendor_name'];
        $rules['vendorPackage'] = $rules['vendor_package'];

        unset($rules['base_namespace']);
        unset($rules['package_name']);
        unset($rules['route_prefix']);
        unset($rules['vendor_name']);
        unset($rules['vendor_package']);

        return $rules;

    } // rules()


    /**
     *
     * @version     1.0.0 / 2026-05-25
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function save(): void
    {
        $this->validate();

        try {
            $this->_Service->update($this->_Module, [
                'active'         => $this->active,
                'base_namespace' => $this->baseNamespace,
                'label'          => $this->label,
                'package_name'   => $this->packageName,
                'route_prefix'   => $this->routePrefix,
                'vendor_name'    => $this->vendorName,
                'vendor_package' => $this->vendorPackage,
            ]);

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

            \AppLog::info('Module ['.$this->_Module->identifier.'] updated by ['.$User->name.' ('.$User->email.')]', self::$_logPrefix);

            $this->dispatch('init-tippyjs');
            $this->dispatch('module-saved');

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

            $this->dangerBanner(__('globals::modules.error.update-error'));
        }

    } // save()


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


} // class Edit extends Component {}
