<?php

namespace Modules\MedDocuments\Http\Controllers;

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 Documents (Medical) section into the admin
 * sidebar. Mirrors Modules\Vrc\Http\Controllers\DataController.
 */
class DataController extends Controller
{
    /**
     * Module entry for the superadmin package manager.
     */
    public function superadmin_package()
    {
        return [
            [
                'name'    => 'meddoc_module',
                'label'   => __('meddocuments::lang.meddoc_module'),
                'default' => false,
            ],
        ];
    }

    /**
     * MedDocuments permissions exposed in the role editor. (Admins bypass these
     * via the core Gate::before for Admin#<business_id>.) See plan §D.
     */
    public function user_permissions()
    {
        return [
            ['value' => 'meddoc.view',              'label' => __('meddocuments::lang.perm_view'),              'default' => false],
            ['value' => 'meddoc.upload',            'label' => __('meddocuments::lang.perm_upload'),            'default' => false],
            ['value' => 'meddoc.manage_types',      'label' => __('meddocuments::lang.perm_manage_types'),      'default' => false],
            ['value' => 'meddoc.share',             'label' => __('meddocuments::lang.perm_share'),             'default' => false],
            ['value' => 'meddoc.download_original', 'label' => __('meddocuments::lang.perm_download_original'), 'default' => false],
            ['value' => 'meddoc.audit.view',        'label' => __('meddocuments::lang.perm_audit_view'),        'default' => false],
        ];
    }

    /**
     * Inject the "Documents (Medical)" section into the admin sidebar. Gated
     * exactly like Vrc: SaaS entitlement gate (data-check, immune to the
     * Admin#<biz> Gate::before bypass) + a can('meddoc.view') check.
     */
    public function modifyAdminMenu()
    {
        // SaaS entitlement gate — keep the section out of the sidebar in
        // projects not entitled to it, even for Admins who bypass can(). No-op
        // outside SaasSwitcher so the module stays portable.
        if (function_exists('module_entitled') && ! module_entitled('MedDocuments')) {
            return;
        }

        if (! auth()->check() || ! auth()->user()->can('meddoc.view')) {
            return;
        }

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

                $sub->url(route('meddoc.documents.index'), __('meddocuments::lang.browser'),
                    ['active' => request()->segment(1) == 'med-docs' && in_array(request()->segment(2), [null, 'documents'], true)]);

                if (auth()->user()->can('meddoc.manage_types')) {
                    $sub->url(route('meddoc.types.index'), __('meddocuments::lang.doc_types'), ['active' => $isSeg('types')]);
                }

                if (auth()->user()->can('meddoc.audit.view')) {
                    $sub->url(route('meddoc.audit.index'), __('meddocuments::lang.access_audit'), ['active' => $isSeg('audit')]);
                }
            }, ['icon' => 'fa fas fa-folder-open'])->order(39);
        });
    }
}
