<?php
/**
 * Extension Navigation Service Provider Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Providers
 * @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\Providers;


use BplanBase\Globals\Registries\ExtensionRegistry;
use BplanBase\Globals\ViewModels\MenuItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;


/**
 * Extension Navigation Service Provider Class
 *
 * Hängt die in der ExtensionRegistry registrierten Navigationseinträge von
 * Extension-Paketen (Components) ans Ende der Sidebar des jeweils aktiven
 * Moduls. Läuft nach den modul-eigenen NavigationServiceProvidersn.
 *
 * @version     1.1.0 / 2026-06-08
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class ExtensionNavigationServiceProvider extends ServiceProvider
{


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


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


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


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


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


    /**
     *
     * @param       Request $Request
     *
     * @return      void
     *
     * @version     1.1.0 / 2026-06-08
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function boot(Request $Request): void
    {
        View::composer('globals::layouts.module-frame.sidebar', function ($View) use ($Request) {
            $currentModule = \Registry::get('env.currentModule');

            if ($currentModule === null) {
                return;
            }
            $extensions = ExtensionRegistry::allForModule($currentModule);

            if (empty($extensions) === true) {
                return;
            }
            $User = Auth::user();
            $extensionItems = [];

            foreach ($extensions as $entry) {
                /*
                **  Permission-Filterung auf Elterneintrag-Ebene. */
                if ($entry['permission'] !== null && $User !== null) {
                    if ($User->can($entry['permission']) === false) {
                        continue;
                    }
                }
                $children = [];
                $parentActive = false;

                foreach ($entry['items'] as $item) {
                    /*
                    **  Permission-Filterung auf Kindelement-Ebene. */
                    if (isset($item['permission']) === true && $item['permission'] !== null && $User !== null) {
                        if ($User->can($item['permission']) === false) {
                            continue;
                        }
                    }
                    $active = $Request->routeIs($item['route']) === true;

                    if ($active === true) {
                        $parentActive = true;
                    }
                    $children[] = new MenuItem(
                        active: $active,
                        label: __($item['locale-key']),
                        url: route($item['route']),
                    );
                }
                if (empty($children) === true) {
                    continue;
                }
                $extensionItems[] = new MenuItem(
                    active  : $parentActive,
                    children: $children,
                    icon    : $entry['icon'] ?? svg_icon('visitor-management.svg-icons.more', ' :class="{ \'text-primary-500\': '.($parentActive ? 'true' : 'false').' }"'),
                    label   : __($entry['locale-key']),
                );
            }
            \Registry::put('extensions.sidebar-items', $extensionItems);
        });

    } // boot()


    /**
     *
     * @return      void
     *
     * @version     1.0.0 / 2026-06-06
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function register(): void
    {
        //
    } // register()


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


} // class ExtensionNavigationServiceProvider extends ServiceProvider {}
