<?php

namespace Modules\ClinicLab\Http\Controllers;

use Illuminate\Routing\Controller;

/**
 * ClinicLab DataController — surfaced by the ERP's ModuleUtil::getModuleData()
 * (which only invokes it once cliniclab_version is set in the system table, via
 * InstallController). Adds the P4-T4 lab-upgrade permissions to the role editor
 * and the Laboratory sidebar dropdown (Catalog / Specimens / Reviews / Outreach).
 */
class DataController extends Controller
{
    /**
     * SIFA P4-T4 permissions, surfaced so SaaS package admins can grant them
     * per role. Additive — the legacy /clinic-lab order/results flow keeps
     * riding on ClinicCore/LabCore permissions.
     */
    public function user_permissions()
    {
        return [
            ['value' => 'cliniclab.catalog.manage',  'label' => __('clinicLab::lang.perm_catalog_manage'),  'default' => true],
            ['value' => 'cliniclab.specimen.manage', 'label' => __('clinicLab::lang.perm_specimen_manage'), 'default' => true],
            ['value' => 'cliniclab.result.review',   'label' => __('clinicLab::lang.perm_result_review'),   'default' => true],
            ['value' => 'cliniclab.result.verify',   'label' => __('clinicLab::lang.perm_result_verify'),   'default' => false],
            ['value' => 'cliniclab.outreach.manage', 'label' => __('clinicLab::lang.perm_outreach_manage'), 'default' => false],
        ];
    }

    /**
     * Inject the Laboratory dropdown. Entitlement-gated (DB-driven, immune to the
     * admin Gate::before bypass) so tenants without ClinicLab never see it. Placed
     * after ClinicCore (order 41) — additive, no existing menu is reordered.
     */
    public function modifyAdminMenu()
    {
        if (function_exists('module_entitled') && ! module_entitled('ClinicLab')) {
            return;
        }

        $user = auth()->user();
        if (! $user) {
            return;
        }

        $canCatalog  = $user->can('cliniclab.catalog.manage');
        $canSpecimen = $user->can('cliniclab.specimen.manage');
        $canReview   = $user->can('cliniclab.result.review') || $user->can('cliniclab.result.verify');
        $canOutreach = $user->can('cliniclab.outreach.manage');

        // Also allow the legacy order/results access so the dropdown appears for
        // clinic staff already able to order labs even before new perms are granted.
        $canLegacy = $user->can('clinic.manage_appointments') || $user->can('lab.access_module') || $user->can('clinic.manage_pets');

        if (! ($canCatalog || $canSpecimen || $canReview || $canOutreach || $canLegacy)) {
            return;
        }

        \Menu::modify('admin-sidebar-menu', function ($menu) use ($canCatalog, $canSpecimen, $canReview, $canOutreach, $canLegacy) {
            $menu->dropdown(__('clinicLab::lang.laboratory'), function ($sub) use ($canCatalog, $canSpecimen, $canReview, $canOutreach, $canLegacy) {
                $seg2 = fn ($seg) => request()->segment(1) === 'clinic-lab' && request()->segment(2) === $seg;

                if ($canLegacy || $canSpecimen || $canReview) {
                    $sub->url(route('clinic-lab.index'), __('clinicLab::lang.worklist'),
                        ['active' => request()->segment(1) === 'clinic-lab' && ! request()->segment(2)]);
                }
                if ($canSpecimen) {
                    $sub->url(route('clinic-lab.specimens.index'), __('clinicLab::lang.specimens'),
                        ['active' => $seg2('specimens')]);
                }
                if ($canReview) {
                    $sub->url(route('clinic-lab.reviews.index'), __('clinicLab::lang.reviews'),
                        ['active' => $seg2('reviews')]);
                }
                if ($canCatalog) {
                    $sub->url(route('clinic-lab.catalog.index'), __('clinicLab::lang.catalog'),
                        ['active' => $seg2('catalog')]);
                }
                if ($canOutreach) {
                    $sub->url(route('clinic-lab.outreach.index'), __('clinicLab::lang.outreach'),
                        ['active' => $seg2('outreach')]);
                }
            }, ['icon' => 'fas fa-flask'])->order(41);
        });
    }
}
