<?php

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

    /**
     * MedAccounting 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_06_0006_register_medaccounting_permissions.
     */
    public function user_permissions()
    {
        $m = __('medaccounting::lang.medacc_module');

        return [
            ['value' => 'medacc.coa.manage',         'label' => $m . ' — ' . __('medaccounting::lang.perm_coa_manage'),         'default' => false],
            ['value' => 'medacc.journal.manage',     'label' => $m . ' — ' . __('medaccounting::lang.perm_journal_manage'),     'default' => false],
            ['value' => 'medacc.journal.post',       'label' => $m . ' — ' . __('medaccounting::lang.perm_journal_post'),       'default' => false],
            ['value' => 'medacc.periods.manage',     'label' => $m . ' — ' . __('medaccounting::lang.perm_periods_manage'),     'default' => false],
            ['value' => 'medacc.reports.view',       'label' => $m . ' — ' . __('medaccounting::lang.perm_reports_view'),       'default' => false],
            ['value' => 'medacc.costcenters.manage', 'label' => $m . ' — ' . __('medaccounting::lang.perm_costcenters_manage'), 'default' => false],
        ];
    }

    /**
     * Inject the "Accounting" section into the admin sidebar (Chart of Accounts /
     * Journal Entries / Cost Centers / Periods / Reports), gated EXACTLY like
     * MedBilling: the SaaS entitlement helper (guarded with function_exists so
     * single-tenant hosts where module_entitled is undefined default to allowed),
     * the medaccounting_module subscription flag and the medacc.* permissions.
     */
    public function modifyAdminMenu()
    {
        // On single-tenant, module_entitled may be undefined — guard fail-soft.
        if (function_exists('module_entitled') && ! module_entitled('MedAccounting')) {
            return;
        }

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

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

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

        $can_coa      = auth()->user()->can('medacc.coa.manage');
        $can_journal  = auth()->user()->can('medacc.journal.manage');
        $can_cc       = auth()->user()->can('medacc.costcenters.manage');
        $can_periods  = auth()->user()->can('medacc.periods.manage');
        $can_reports  = auth()->user()->can('medacc.reports.view');

        if (! $can_coa && ! $can_journal && ! $can_cc && ! $can_periods && ! $can_reports) {
            return;
        }

        \Menu::modify('admin-sidebar-menu', function ($menu) use ($can_coa, $can_journal, $can_cc, $can_periods, $can_reports) {
            $menu->dropdown(__('medaccounting::lang.accounting'), function ($sub) use ($can_coa, $can_journal, $can_cc, $can_periods, $can_reports) {
                $isSeg = fn ($seg) => request()->segment(1) == 'med-accounting' && request()->segment(2) == $seg;

                if ($can_coa) {
                    $sub->url(route('medacc.coa.index'), __('medaccounting::lang.chart_of_accounts'),
                        ['active' => $isSeg('coa')]);
                }
                if ($can_journal) {
                    $sub->url(route('medacc.journal.index'), __('medaccounting::lang.journal_entries'),
                        ['active' => $isSeg('journal')]);
                }
                if ($can_cc) {
                    $sub->url(route('medacc.costcenters.index'), __('medaccounting::lang.cost_centers'),
                        ['active' => $isSeg('cost-centers')]);
                }
                if ($can_periods) {
                    $sub->url(route('medacc.periods.index'), __('medaccounting::lang.periods'),
                        ['active' => $isSeg('periods')]);
                }
                if ($can_reports) {
                    $sub->url(route('medacc.reports.index'), __('medaccounting::lang.reports'),
                        ['active' => $isSeg('reports')]);
                }
            }, ['icon' => 'fa fas fa-balance-scale'])->order(46);
        });
    }
}
