<?php

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

    /**
     * MedWorkforce 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_07_0005_register_medworkforce_permissions.
     */
    public function user_permissions()
    {
        $m = __('medworkforce::lang.medworkforce_module');

        return [
            ['value' => 'medwf.roster.manage',      'label' => $m . ' — ' . __('medworkforce::lang.perm_roster_manage'),      'default' => false],
            ['value' => 'medwf.roster.view',        'label' => $m . ' — ' . __('medworkforce::lang.perm_roster_view'),        'default' => false],
            ['value' => 'medwf.swap.approve',       'label' => $m . ' — ' . __('medworkforce::lang.perm_swap_approve'),       'default' => false],
            ['value' => 'medwf.credentials.view',   'label' => $m . ' — ' . __('medworkforce::lang.perm_credentials_view'),   'default' => false],
            ['value' => 'medwf.credentials.manage', 'label' => $m . ' — ' . __('medworkforce::lang.perm_credentials_manage'), 'default' => false],
        ];
    }

    /**
     * Inject the "Workforce" section into the admin sidebar (Duty Roster / Shift
     * Templates / Swap Requests / Credential Compliance), gated EXACTLY like
     * MedBilling: the SaaS entitlement helper, the medworkforce_module
     * subscription flag and the medwf.* permissions.
     */
    public function modifyAdminMenu()
    {
        if (function_exists('module_entitled') && ! module_entitled('MedWorkforce')) {
            return;
        }

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

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

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

        $can_roster_view = auth()->user()->can('medwf.roster.view') || auth()->user()->can('medwf.roster.manage');
        $can_roster_manage = auth()->user()->can('medwf.roster.manage');
        $can_swap = auth()->user()->can('medwf.swap.approve');
        $can_cred = auth()->user()->can('medwf.credentials.view') || auth()->user()->can('medwf.credentials.manage');

        if (! $can_roster_view && ! $can_roster_manage && ! $can_swap && ! $can_cred) {
            return;
        }

        \Menu::modify('admin-sidebar-menu', function ($menu) use ($can_roster_view, $can_roster_manage, $can_swap, $can_cred) {
            $menu->dropdown(__('medworkforce::lang.workforce'), function ($sub) use ($can_roster_view, $can_roster_manage, $can_swap, $can_cred) {
                $isSeg = fn ($seg) => request()->segment(1) == 'med-workforce' && request()->segment(2) == $seg;

                if ($can_roster_view) {
                    $sub->url(route('medwf.roster.index'), __('medworkforce::lang.duty_roster'),
                        ['active' => $isSeg('roster')]);
                }
                if ($can_roster_manage) {
                    $sub->url(route('medwf.templates.index'), __('medworkforce::lang.shift_templates'),
                        ['active' => $isSeg('templates')]);
                }
                if ($can_swap) {
                    $sub->url(route('medwf.swaps.index'), __('medworkforce::lang.swap_requests'),
                        ['active' => $isSeg('swaps')]);
                }
                if ($can_cred) {
                    $sub->url(route('medwf.compliance.index'), __('medworkforce::lang.credential_compliance'),
                        ['active' => $isSeg('compliance')]);
                }
            }, ['icon' => 'fa fas fa-user-nurse'])->order(47);
        });
    }
}
