<?php

namespace Modules\AiCore\Http\Controllers;

use Illuminate\Routing\Controller;
use Menu;

/**
 * Core ERP integration hooks. The ERP discovers this class by convention
 * (Modules\<Module>\Http\Controllers\DataController) and calls these methods
 * via ModuleUtil / the AdminSidebarMenu middleware.
 */
class DataController extends Controller
{
    public function superadmin_package()
    {
        return [
            [
                'name'    => 'ai_core',
                'label'   => __('aiCore::lang.ai_core_module'),
                'default' => false,
            ],
        ];
    }

    public function user_permissions()
    {
        return [
            ['value' => 'aicore.access_module',   'label' => __('aiCore::lang.perm_access_module'),   'default' => false],
            ['value' => 'aicore.access_settings', 'label' => __('aiCore::lang.perm_access_settings'), 'default' => false],
            ['value' => 'aicore.manage',          'label' => __('aiCore::lang.perm_manage'),          'default' => false],
            ['value' => 'aicore.view_usage',      'label' => __('aiCore::lang.perm_view_usage'),      'default' => false],
        ];
    }

    /** Inject the AI Core dropdown into the admin sidebar. */
    public function modifyAdminMenu()
    {
        $user = auth()->user();
        if (!$user) {
            return;
        }

        $canAccess = $user->can('aicore.access_module')
            || $user->can('aicore.access_settings')
            || $user->can('aicore.manage')
            || $user->can('aicore.view_usage');

        if (!$canAccess) {
            return;
        }

        Menu::modify('admin-sidebar-menu', function ($menu) use ($user) {
            $menu->dropdown(__('aiCore::lang.ai_core'), function ($sub) use ($user) {

                $sub->url(
                    route('aicore.settings.index'),
                    '⚙ ' . __('aiCore::lang.ai_settings'),
                    ['active' => request()->segment(1) == 'ai-core' && request()->segment(2) == 'settings']
                );

                if ($user->can('aicore.view_usage') || $user->can('aicore.manage') || $user->can('aicore.access_settings')) {
                    $sub->url(
                        route('aicore.usage'),
                        '📊 AI Usage',
                        ['active' => request()->segment(1) == 'ai-core' && request()->segment(2) == 'usage']
                    );
                }

            }, ['icon' => 'fas fa-robot'])->order(6);
        });
    }
}
