<?php

namespace Modules\NX600Lab\Http\Controllers;

use Illuminate\Routing\Controller;

/**
 * Core ERP integration hooks. The ERP discovers this class by convention
 * (Modules\<Module>\Http\Controllers\DataController) and calls these methods
 * via ModuleUtil::getModuleData().
 */
class DataController extends Controller
{
    /**
     * Registers the module in the Superadmin package/feature list.
     */
    public function superadmin_package()
    {
        return [
            [
                'name'    => 'nx600_lab',
                'label'   => __('nx600Lab::lang.nx600_lab_module'),
                'default' => false,
            ],
        ];
    }

    /**
     * Permissions exposed in the Roles screen.
     */
    public function user_permissions()
    {
        return [
            ['value' => 'nx600_lab.access_module',  'label' => __('nx600Lab::lang.perm_access_module'),  'default' => false],
            ['value' => 'nx600_lab.manage_worklist','label' => __('nx600Lab::lang.perm_manage_worklist'),'default' => false],
            ['value' => 'nx600_lab.view_results',   'label' => __('nx600Lab::lang.perm_view_results'),   'default' => false],
            ['value' => 'nx600_lab.manage_results', 'label' => __('nx600Lab::lang.perm_manage_results'), 'default' => false],
            ['value' => 'nx600_lab.manage_patients','label' => __('nx600Lab::lang.perm_manage_patients'),'default' => false],
            ['value' => 'nx600_lab.view_devices',   'label' => __('nx600Lab::lang.perm_view_devices'),   'default' => false],
            ['value' => 'nx600_lab.view_logs',      'label' => __('nx600Lab::lang.perm_view_logs'),      'default' => false],
            ['value' => 'nx600_lab.access_settings','label' => __('nx600Lab::lang.perm_access_settings'),'default' => false],
        ];
    }

    /**
     * Inject the NX600 Lab dropdown into the admin sidebar.
     */
    public function modifyAdminMenu()
    {
        $user = auth()->user();
        if (!$user || !$user->can('nx600_lab.access_module')) {
            return;
        }
        // Per-business module entitlement (DB-driven, admin-bypass-immune).
        if (!module_entitled('NX600Lab')) {
            return;
        }

        \Menu::modify('admin-sidebar-menu', function ($menu) use ($user) {
            $menu->dropdown(__('nx600Lab::lang.nx600_lab'), function ($sub) use ($user) {

                $sub->url(
                    route('nx.dashboard'),
                    __('nx600Lab::lang.dashboard'),
                    ['active' => request()->segment(1) == 'nx600' && !request()->segment(2)]
                );

                if ($user->can('nx600_lab.manage_worklist')) {
                    $sub->url(
                        route('nx.worklist.index'),
                        __('nx600Lab::lang.worklist'),
                        ['active' => request()->segment(2) == 'worklist']
                    );
                }

                if ($user->can('nx600_lab.view_results')) {
                    $sub->url(
                        route('nx.results.index'),
                        __('nx600Lab::lang.test_results'),
                        ['active' => request()->segment(2) == 'results']
                    );
                }

                if ($user->can('nx600_lab.manage_patients')) {
                    $sub->url(
                        route('nx.patients.index'),
                        __('nx600Lab::lang.patients'),
                        ['active' => request()->segment(2) == 'patients']
                    );
                }

                if ($user->can('nx600_lab.view_devices')) {
                    $sub->url(
                        route('nx.devices.index'),
                        __('nx600Lab::lang.device_monitor'),
                        ['active' => request()->segment(2) == 'devices']
                    );
                }

                if ($user->can('nx600_lab.view_logs')) {
                    $sub->url(
                        route('nx.logs.index'),
                        __('nx600Lab::lang.communication_logs'),
                        ['active' => request()->segment(2) == 'logs']
                    );
                }

                if ($user->can('nx600_lab.access_settings')) {
                    $sub->url(
                        route('nx.settings.index'),
                        '⚙ ' . __('nx600Lab::lang.settings'),
                        ['active' => request()->segment(2) == 'settings']
                    );
                }

            }, ['icon' => 'fas fa-vials'])->order(41);
        });
    }
}
