<?php

namespace Modules\WhatsApp\Http\Controllers;

use Illuminate\Routing\Controller;
use Menu;

/**
 * Module hook controller. The core AdminSidebarMenu middleware calls
 * modifyAdminMenu() on every installed module's DataController.
 */
class DataController extends Controller
{
    public function modifyAdminMenu()
    {
        $user = auth()->user();
        if (!$user) {
            return;
        }

        $canAccess = $user->can('whatsapp.access_module')
            || $user->can('whatsapp.manage_channels')
            || $user->can('whatsapp.view_messages')
            || $user->can('whatsapp.access_settings');

        if (!$canAccess) {
            return;
        }

        Menu::modify('admin-sidebar-menu', function ($menu) use ($user) {
            $menu->dropdown(
                __('whatsapp::lang.whatsapp'),
                function ($sub) use ($user) {
                    $sub->url(
                        action([\Modules\WhatsApp\Http\Controllers\WaDashboardController::class, 'index']),
                        __('whatsapp::lang.dashboard'),
                        ['active' => request()->is('whatsapp')]
                    );

                    if ($user->can('whatsapp.view_messages')) {
                        $inboxLabel = __('whatsapp::lang.inbox_chat');
                        try {
                            $unread = (int) \Modules\WhatsApp\Entities\WaConversation::where('business_id', session('user.business_id'))->sum('unread_count');
                            if ($unread > 0) {
                                $inboxLabel .= ' <span class="badge" style="background:#25d366;">' . $unread . '</span>';
                            }
                        } catch (\Throwable $e) {
                        }
                        $sub->url(
                            action([\Modules\WhatsApp\Http\Controllers\WaInboxController::class, 'index']),
                            $inboxLabel,
                            ['active' => request()->is('whatsapp/inbox*')]
                        );
                    }

                    if ($user->can('whatsapp.manage_channels')) {
                        $sub->url(
                            action([\Modules\WhatsApp\Http\Controllers\WaChannelController::class, 'index']),
                            __('whatsapp::lang.channels'),
                            ['active' => request()->is('whatsapp/channels*')]
                        );
                    }

                    if ($user->can('whatsapp.manage_templates')) {
                        $sub->url(
                            action([\Modules\WhatsApp\Http\Controllers\WaTemplateController::class, 'index']),
                            __('whatsapp::lang.templates'),
                            ['active' => request()->is('whatsapp/templates*')]
                        );
                        $sub->url(
                            action([\Modules\WhatsApp\Http\Controllers\WaWorkflowController::class, 'index']),
                            __('whatsapp::lang.workflow'),
                            ['active' => request()->is('whatsapp/workflow*')]
                        );
                    }

                    if ($user->can('whatsapp.view_messages')) {
                        $sub->url(
                            action([\Modules\WhatsApp\Http\Controllers\WaMessageController::class, 'index']),
                            __('whatsapp::lang.outbox'),
                            ['active' => request()->is('whatsapp/messages*')]
                        );
                    }

                    if ($user->can('whatsapp.access_settings')) {
                        $sub->url(
                            action([\Modules\WhatsApp\Http\Controllers\WaSettingsController::class, 'index']),
                            __('whatsapp::lang.settings'),
                            ['active' => request()->is('whatsapp/settings*')]
                        );
                    }
                },
                ['icon' => 'fab fa-whatsapp']
            )->order(7);
        });

        return;
    }
}
