<?php

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

    /**
     * MedBloodBank 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_8005_register_medbloodbank_permissions.
     */
    public function user_permissions()
    {
        $m = __('medbloodbank::lang.medbloodbank_module');

        return [
            ['value' => 'mbb.donors.manage',      'label' => $m . ' — ' . __('medbloodbank::lang.perm_donors_manage'),      'default' => false],
            ['value' => 'mbb.donations.manage',   'label' => $m . ' — ' . __('medbloodbank::lang.perm_donations_manage'),   'default' => false],
            ['value' => 'mbb.screening.result',   'label' => $m . ' — ' . __('medbloodbank::lang.perm_screening_result'),   'default' => false],
            ['value' => 'mbb.crossmatch.perform', 'label' => $m . ' — ' . __('medbloodbank::lang.perm_crossmatch_perform'), 'default' => false],
            ['value' => 'mbb.issue.manage',       'label' => $m . ' — ' . __('medbloodbank::lang.perm_issue_manage'),       'default' => false],
        ];
    }

    /**
     * Inject the "Blood Bank" section into the admin sidebar (Dashboard / Donors /
     * Donations / Crossmatch / Issues), gated EXACTLY like MedBilling: the SaaS
     * entitlement helper, the medbloodbank_module subscription flag and the mbb.*
     * permissions.
     */
    public function modifyAdminMenu()
    {
        if (function_exists('module_entitled') && ! module_entitled('MedBloodBank')) {
            return;
        }

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

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

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

        $can_donors     = auth()->user()->can('mbb.donors.manage');
        $can_donations  = auth()->user()->can('mbb.donations.manage');
        $can_screening  = auth()->user()->can('mbb.screening.result');
        $can_crossmatch = auth()->user()->can('mbb.crossmatch.perform');
        $can_issue      = auth()->user()->can('mbb.issue.manage');

        // Anyone with any blood-bank permission sees the dashboard.
        $can_any = $can_donors || $can_donations || $can_screening || $can_crossmatch || $can_issue;
        if (! $can_any) {
            return;
        }

        \Menu::modify('admin-sidebar-menu', function ($menu) use ($can_donors, $can_donations, $can_crossmatch, $can_issue, $can_any) {
            $menu->dropdown(__('medbloodbank::lang.blood_bank'), function ($sub) use ($can_donors, $can_donations, $can_crossmatch, $can_issue, $can_any) {
                $isSeg = fn ($seg) => request()->segment(1) == 'med-blood-bank' && request()->segment(2) == $seg;

                if ($can_any) {
                    $sub->url(route('mbb.dashboard'), __('medbloodbank::lang.dashboard'),
                        ['active' => request()->segment(1) == 'med-blood-bank' && ! request()->segment(2)]);
                }
                if ($can_donors) {
                    $sub->url(route('mbb.donors.index'), __('medbloodbank::lang.donors'),
                        ['active' => $isSeg('donors')]);
                }
                if ($can_donations) {
                    $sub->url(route('mbb.donations.index'), __('medbloodbank::lang.donations'),
                        ['active' => $isSeg('donations')]);
                }
                if ($can_crossmatch) {
                    $sub->url(route('mbb.crossmatch.index'), __('medbloodbank::lang.crossmatch'),
                        ['active' => $isSeg('crossmatch')]);
                }
                if ($can_issue) {
                    $sub->url(route('mbb.issues.index'), __('medbloodbank::lang.issues'),
                        ['active' => $isSeg('issues')]);
                }
            }, ['icon' => 'fa fas fa-tint'])->order(42);
        });
    }
}
