from .models import SidebarMenuItem

def build_menu_tree(parent=None):
    items = SidebarMenuItem.objects.filter(parent=parent).order_by("order")
    tree = []

    for item in items:
        tree.append({
            "type": item.type,
            "label": item.label,
            "icon": item.icon,
            "url": item.url,
            "badge": item.badge,
            "children": build_menu_tree(parent=item)
        })

    return tree

def sidebar_context(request):
    sidebar_menu = build_menu_tree()
    return {"sidebar_menu": sidebar_menu}
