<?php

namespace Modules\VetAdmission\Http\Controllers;

use Illuminate\Routing\Controller;

/**
 * Core ERP integration hooks for VetAdmission.
 *
 * VetAdmission does NOT register its own top-level sidebar dropdown. Instead it
 * contributes sub-items to the VetClinic dropdown via vetClinicMenuItems(),
 * which VetClinic's DataController collects and renders inline.
 */
class DataController extends Controller
{
    public function superadmin_package()
    {
        return [
            [
                'name'    => 'vet_admission',
                'label'   => __('vetAdmission::lang.vet_admission_module'),
                'default' => false,
            ],
        ];
    }

    public function user_permissions()
    {
        return [
            ['value' => 'vet_admission.access_module',    'label' => 'Vet Admission: Access Module',                'default' => false],
            ['value' => 'vet_admission.view_admissions',  'label' => 'Vet Admission: View Admissions',              'default' => false],
            ['value' => 'vet_admission.manage_admissions','label' => 'Vet Admission: Manage Admissions & Discharge','default' => false],
            ['value' => 'vet_admission.log_care',         'label' => 'Vet Admission: Log Care Tasks & Vitals',      'default' => false],
            ['value' => 'vet_admission.manage_settings',  'label' => 'Vet Admission: Manage Wards & Care Tasks',    'default' => false],
        ];
    }

    /**
     * Sidebar contribution rendered INSIDE the VetClinic dropdown.
     * VetClinic's DataController::modifyAdminMenu() collects this closure and
     * invokes it with the dropdown sub-builder.
     */
    public function vetClinicMenuItems()
    {
        return function ($sub) {
            if (!module_entitled('VetAdmission')) return;
            $user = auth()->user();
            if (!$user || !$user->can('vet_admission.access_module')) {
                return;
            }

            if ($user->can('vet_admission.view_admissions') || $user->can('vet_admission.manage_admissions')) {
                $sub->url(
                    route('va.admissions.index'),
                    '🏥 ' . __('vetAdmission::lang.admissions'),
                    ['active' => request()->segment(1) == 'vet-admission' && request()->segment(2) == 'admissions']
                );
                $sub->url(
                    route('va.admissions.board'),
                    '🏥 ' . __('vetAdmission::lang.ward_board'),
                    ['active' => request()->segment(1) == 'vet-admission' && request()->segment(2) == 'ward-board']
                );
            }

            if ($user->can('vet_admission.manage_settings')
                || $user->can('vet_admission.manage_admissions'))
            {
                $sub->url(
                    route('va.protocols.index'),
                    '⚙ Treatment Protocols',
                    ['active' => request()->segment(1) == 'vet-admission' && request()->segment(2) == 'protocols']
                );
            }

            if ($user->can('vet_admission.manage_settings')) {
                $sub->url(
                    route('va.wards.index'),
                    '⚙ ' . __('vetAdmission::lang.wards'),
                    ['active' => request()->segment(1) == 'vet-admission' && request()->segment(2) == 'wards']
                );
                $sub->url(
                    route('va.care-task-types.index'),
                    '⚙ ' . __('vetAdmission::lang.care_task_types'),
                    ['active' => request()->segment(1) == 'vet-admission' && request()->segment(2) == 'care-task-types']
                );
            }
        };
    }
}
