<?php

namespace Modules\MedSurgery\Http\Controllers;

use App\Utils\ModuleUtil;
use Illuminate\Routing\Controller;

/**
 * Module discovery hooks. The ERP core auto-calls these via
 * App\Utils\ModuleUtil::getModuleData('<method>') — superadmin_package() for the
 * package list, user_permissions() for the role editor and modifyAdminMenu() to
 * inject the "Surgery" section into the admin sidebar. Mirrors
 * Modules\MedBilling\Http\Controllers\DataController.
 *
 * getModuleData() only reaches modules that are enabled AND installed
 * (system.medsurgery_version), so everything here is implicitly install-gated.
 */
class DataController extends Controller
{
    /** Module entry for the superadmin package manager. */
    public function superadmin_package()
    {
        return [
            [
                'name'    => 'medsurgery_module',
                'label'   => __('medsurgery::lang.medsurgery_module'),
                'default' => false,
            ],
        ];
    }

    /**
     * MedSurgery permissions exposed in the role editor. (Admins bypass these via
     * the core Gate::before for Admin#<business_id>.) The same list is registered
     * as Spatie permissions by migration
     * 2026_07_05_7004_register_medsurgery_permissions.
     */
    public function user_permissions()
    {
        $m = __('medsurgery::lang.medsurgery_module');

        return [
            ['value' => 'medsx.case.manage',     'label' => $m . ' — ' . __('medsurgery::lang.perm_case_manage'),     'default' => false],
            ['value' => 'medsx.case.operate',    'label' => $m . ' — ' . __('medsurgery::lang.perm_case_operate'),    'default' => false],
            ['value' => 'medsx.checklist.sign',  'label' => $m . ' — ' . __('medsurgery::lang.perm_checklist_sign'),  'default' => false],
        ];
    }

    /**
     * Inject the "Surgery" section into the admin sidebar (OR board / Cases /
     * Implant registry), gated EXACTLY like MedBilling: the SaaS entitlement
     * helper, the medsurgery_module subscription flag and the medsx.* permissions.
     */
    public function modifyAdminMenu()
    {
        if (function_exists('module_entitled') && ! module_entitled('MedSurgery')) {
            return;
        }

        if (! auth()->check()) {
            return;
        }

        $business_id = session()->get('user.business_id');
        $moduleUtil = new ModuleUtil();

        if (! $moduleUtil->hasThePermissionInSubscription($business_id, 'medsurgery_module')) {
            return;
        }

        $can_manage = auth()->user()->can('medsx.case.manage');
        $can_operate = auth()->user()->can('medsx.case.operate');
        $can_sign = auth()->user()->can('medsx.checklist.sign');

        if (! $can_manage && ! $can_operate && ! $can_sign) {
            return;
        }

        \Menu::modify('admin-sidebar-menu', function ($menu) {
            $menu->dropdown(__('medsurgery::lang.surgery'), function ($sub) {
                $isSeg = fn ($seg) => request()->segment(1) == 'med-surgery' && request()->segment(2) == $seg;

                $sub->url(route('medsx.board.index'), __('medsurgery::lang.or_board'),
                    ['active' => $isSeg('board')]);
                $sub->url(route('medsx.cases.index'), __('medsurgery::lang.cases'),
                    ['active' => $isSeg('cases')]);
                $sub->url(route('medsx.implants.index'), __('medsurgery::lang.implant_registry'),
                    ['active' => $isSeg('implants')]);
            }, ['icon' => 'fa fas fa-user-md'])->order(42);
        });
    }
}
