<?php

namespace Modules\MedWard\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 "Inpatient" section into the admin sidebar. Mirrors
 * Modules\MedBilling\Http\Controllers\DataController.
 *
 * getModuleData() only reaches modules that are enabled AND installed
 * (system.medward_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'    => 'medward_module',
                'label'   => __('medward::lang.medward_module'),
                'default' => false,
            ],
        ];
    }

    /**
     * MedWard 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_5008_register_medward_permissions.
     */
    public function user_permissions()
    {
        $m = __('medward::lang.medward_module');

        return [
            ['value' => 'medward.board.view',         'label' => $m . ' — ' . __('medward::lang.perm_board_view'),         'default' => false],
            ['value' => 'medward.wards.manage',       'label' => $m . ' — ' . __('medward::lang.perm_wards_manage'),       'default' => false],
            ['value' => 'medward.admission.create',   'label' => $m . ' — ' . __('medward::lang.perm_admission_create'),   'default' => false],
            ['value' => 'medward.admission.discharge', 'label' => $m . ' — ' . __('medward::lang.perm_admission_discharge'), 'default' => false],
            ['value' => 'medward.mar.administer',     'label' => $m . ' — ' . __('medward::lang.perm_mar_administer'),     'default' => false],
            ['value' => 'medward.icu.chart',          'label' => $m . ' — ' . __('medward::lang.perm_icu_chart'),          'default' => false],
        ];
    }

    /**
     * Inject the "Inpatient" section into the admin sidebar (Bed board /
     * Admissions / Wards / ICU), gated EXACTLY like MedBilling: the SaaS
     * entitlement helper, the medward_module subscription flag and the medward.*
     * permissions.
     */
    public function modifyAdminMenu()
    {
        if (function_exists('module_entitled') && ! module_entitled('MedWard')) {
            return;
        }

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

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

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

        $can_board = auth()->user()->can('medward.board.view');
        $can_admit = auth()->user()->can('medward.admission.create');
        $can_wards = auth()->user()->can('medward.wards.manage');
        $can_icu = auth()->user()->can('medward.icu.chart');

        if (! $can_board && ! $can_admit && ! $can_wards && ! $can_icu) {
            return;
        }

        \Menu::modify('admin-sidebar-menu', function ($menu) use ($can_board, $can_admit, $can_wards, $can_icu) {
            $menu->dropdown(__('medward::lang.inpatient'), function ($sub) use ($can_board, $can_admit, $can_wards, $can_icu) {
                $isSeg = fn ($seg) => request()->segment(1) == 'med-ward' && request()->segment(2) == $seg;

                if ($can_board) {
                    $sub->url(route('medward.board.index'), __('medward::lang.bed_board'),
                        ['active' => $isSeg('board')]);
                }
                if ($can_admit || $can_board) {
                    $sub->url(route('medward.admissions.index'), __('medward::lang.admissions'),
                        ['active' => $isSeg('admissions')]);
                }
                if ($can_wards) {
                    $sub->url(route('medward.wards.index'), __('medward::lang.wards'),
                        ['active' => $isSeg('wards')]);
                }
                if ($can_icu || $can_board) {
                    $sub->url(route('medward.icu.index'), __('medward::lang.icu_dashboard'),
                        ['active' => $isSeg('icu')]);
                }
            }, ['icon' => 'fa fas fa-procedures'])->order(42);
        });
    }
}
