<?php
class MenuBuilder
{
    public static function render(): string
    {
        $menuItems = ModuleRegistry::getMenuItems();
        $currentUri = (new Request())->uri();
        $html = '';

        foreach ($menuItems as $item) {
            // Check top-level permission
            if (isset($item['permission']) && !Gate::can($item['permission'])) {
                continue;
            }

            $hasChildren = !empty($item['children']);
            $icon = $item['icon'] ?? 'fas fa-circle';
            $labelEn = $item['label_en'] ?? '';
            $labelAr = $item['label_ar'] ?? '';
            $label = Lang::current() === 'ar' ? $labelAr : $labelEn;

            if ($hasChildren) {
                $visibleChildren = [];
                foreach ($item['children'] as $child) {
                    if (isset($child['permission']) && !Gate::can($child['permission'])) {
                        continue;
                    }
                    $visibleChildren[] = $child;
                }

                if (empty($visibleChildren)) continue;

                $isOpen = false;
                foreach ($visibleChildren as $child) {
                    if (isset($child['route']) && str_starts_with($currentUri, $child['route'])) {
                        $isOpen = true;
                        break;
                    }
                }

                $collapseId = 'menu-' . md5($label);
                $showClass = $isOpen ? ' show' : '';
                $collapsedClass = $isOpen ? '' : ' collapsed';

                $html .= '<li class="nav-item">';
                $html .= '<a class="nav-link sidebar-link' . $collapsedClass . '" href="#' . $collapseId . '" data-bs-toggle="collapse" aria-expanded="' . ($isOpen ? 'true' : 'false') . '">';
                $html .= '<i class="' . e($icon) . ' me-2"></i>';
                $html .= '<span>' . e($label) . '</span>';
                $html .= '<i class="fas fa-chevron-down ms-auto sidebar-chevron"></i>';
                $html .= '</a>';
                $html .= '<div class="collapse' . $showClass . '" id="' . $collapseId . '">';
                $html .= '<ul class="nav flex-column ms-3 sidebar-submenu">';

                foreach ($visibleChildren as $child) {
                    $childLabel = Lang::current() === 'ar' ? ($child['label_ar'] ?? '') : ($child['label_en'] ?? '');
                    $childRoute = $child['route'] ?? '#';
                    $activeClass = str_starts_with($currentUri, $childRoute) ? ' active' : '';
                    $html .= '<li class="nav-item">';
                    $html .= '<a class="nav-link sidebar-sublink' . $activeClass . '" href="' . e($childRoute) . '">';
                    $html .= '<i class="fas fa-circle fa-xs me-2"></i>' . e($childLabel);
                    $html .= '</a></li>';
                }

                $html .= '</ul></div></li>';
            } else {
                $route = $item['route'] ?? '#';
                $activeClass = str_starts_with($currentUri, $route) ? ' active' : '';
                $html .= '<li class="nav-item">';
                $html .= '<a class="nav-link sidebar-link' . $activeClass . '" href="' . e($route) . '">';
                $html .= '<i class="' . e($icon) . ' me-2"></i>';
                $html .= '<span>' . e($label) . '</span>';
                $html .= '</a></li>';
            }
        }

        return $html;
    }
}