<?php

namespace Modules\VetAI\Http\Controllers;

use Illuminate\Routing\Controller;

/**
 * Core ERP integration hooks for VetAI.
 *
 * VetAI contributes its sub-items to the VetClinic sidebar dropdown via
 * vetClinicMenuItems(), which VetClinic's DataController collects and renders.
 */
class DataController extends Controller
{
    public function superadmin_package()
    {
        return [
            [
                'name'    => 'vet_ai',
                'label'   => __('vetAI::lang.vet_ai_module'),
                'default' => false,
            ],
        ];
    }

    public function user_permissions()
    {
        return [
            ['value' => 'vet_ai.access_module',   'label' => 'Vet AI: Access Module',                'default' => false],
            ['value' => 'vet_ai.use_assistant',   'label' => 'Vet AI: Use the Clinical Assistant',   'default' => false],
            ['value' => 'vet_ai.view_history',    'label' => 'Vet AI: View AI Query History',        'default' => false],
            ['value' => 'vet_ai.manage_settings', 'label' => 'Vet AI: Manage AI Settings & API Key', 'default' => false],
        ];
    }

    /**
     * Sidebar contribution rendered INSIDE the VetClinic dropdown.
     */
    public function vetClinicMenuItems()
    {
        return function ($sub) {
            if (!module_entitled('VetAI')) return;
            $user = auth()->user();
            if (!$user || !$user->can('vet_ai.access_module')) {
                return;
            }

            if ($user->can('vet_ai.use_assistant')) {
                $sub->url(
                    route('vai.assistant.index'),
                    '🤖 ' . __('vetAI::lang.ai_assistant'),
                    ['active' => request()->segment(1) == 'vet-ai' && request()->segment(2) == 'assistant']
                );
            }

            if ($user->can('vet_ai.view_history')) {
                $sub->url(
                    route('vai.queries.index'),
                    '🤖 ' . __('vetAI::lang.ai_history'),
                    ['active' => request()->segment(1) == 'vet-ai' && request()->segment(2) == 'history']
                );
            }

            if ($user->can('vet_ai.manage_settings')) {
                // Unified config home: vet AI now runs on the shared AiCore
                // engine, so the Settings link points at AiCore's settings page
                // (provider + voice) rather than VetAI's legacy vai_settings UI.
                $sub->url(
                    route('aicore.settings.index'),
                    '⚙ ' . __('vetAI::lang.ai_settings'),
                    ['active' => request()->segment(1) == 'ai-core' && request()->segment(2) == 'settings']
                );
            }
        };
    }
}
