<?php

use Modules\Menu\Models\MenusModel;
use Modules\Menu\Models\MenusCategoryModel;

if (!function_exists('generate_menu')) {
    function generate_menu($menuType = 'main-menu') 
    {
        $menuCategoryModel = new MenusCategoryModel();
        $menuModel = new MenusModel();
        
        $menuType = $menuCategoryModel->where('alias', $menuType)->first();
        if (!$menuType) return [];
        
        $menuItems = $menuModel->where('category_id', $menuType['id'])
                              ->where('status', 1)
                              ->orderBy('created_at', 'asc')
                              ->findAll();
        
        if (empty($menuItems)) return [];
        
        $menu = [];
        foreach ($menuItems as $item) {
            $itemArray = is_object($item) ? $item->toArray() : $item;
            
            if ($itemArray['level'] == 1) {
                $menu[$itemArray['id']] = $itemArray;
                $menu[$itemArray['id']]['children'] = [];
            } elseif ($itemArray['level'] == 2 && isset($menu[$itemArray['parent_id']])) {
                $menu[$itemArray['parent_id']]['children'][$itemArray['id']] = $itemArray;
                $menu[$itemArray['parent_id']]['children'][$itemArray['id']]['children'] = [];
            } elseif ($itemArray['level'] == 3) {
                foreach ($menu as &$parent) {
                    if (isset($parent['children'][$itemArray['parent_id']])) {
                        $parent['children'][$itemArray['parent_id']]['children'][] = $itemArray;
                    }
                }
            }
        }
        
        return $menu;
    }
}

if (!function_exists('render_menu_item')) {
    function render_menu_item($item)
    {
        ob_start(); ?>
        <li class="<?= !empty($item['children']) ? 'has-dropdown' : '' ?>">
            <a href="<?= esc($item['link'] ?? '#') ?>">
                <?= esc($item['title'] ?? 'Untitled') ?>
                <?php if (!empty($item['children'])): ?>
                    <i class="fas fa-chevron-down mobile-dropdown-toggle"></i>
                <?php endif; ?>
            </a>

            <?php if (!empty($item['children'])): ?>
                <ul class="dropdown">
                    <?php foreach ($item['children'] as $child): ?>
                        <li>
                            <a href="<?= esc($child['link'] ?? '#') ?>">
                                <?= esc($child['title'] ?? 'Untitled') ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </li>
        <?php return ob_get_clean();
    }
}

if (!function_exists('render_menu')) {
    function render_menu($menu = [], $logoUrl = '', $logoLink = '/')
    {
        if (!is_array($menu) || empty($menu)) return '';

        $menuCount = count($menu);
        $half = ceil($menuCount / 2);
        $leftMenu = array_slice($menu, 0, $half, true);
        $rightMenu = array_slice($menu, $half, null, true);

        ob_start(); ?>
        
        <!-- Main Navigation -->
        <div class="main-nav">
            <!-- Mobile Menu Button -->
            <button class="mobile-menu-btn" id="mobileMenuBtn">
                <i class="fas fa-bars"></i>
            </button>
            
            <!-- Mobile Icons -->
            <div class="mobile-icons">
                <a href="#" aria-label="Search" id="mobileSearchBtn"><i class="fas fa-search"></i></a>  
                <a href="#" class="account-icon" aria-label="Account" id="mobileAccountBtn"><i class="fas fa-user"></i></a>
            </div>
            
            <!-- Logo that cuts through both menus -->
            <div class="logo-container">
                <div class="logo">
                    <a href="<?= esc($logoLink) ?>">
                        <img src="<?= esc($logoUrl) ?>" alt="Site Logo">
                    </a>
                </div>
            </div>
            
            <!-- Left Navigation -->
            <div class="nav-section nav-left">
                <ul class="nav-links">
                    <?php foreach ($leftMenu as $item): ?>
                        <?= render_menu_item($item) ?>
                    <?php endforeach; ?>
                </ul>
            </div>
            
            <!-- Right Navigation -->
            <div class="nav-section nav-right">
                <ul class="nav-links">
                    <?php foreach ($rightMenu as $item): ?>
                        <?= render_menu_item($item) ?>
                    <?php endforeach; ?>
                </ul>

                <!-- Search and Account Icons -->
                <div class="nav-icons">
                    <a href="#" class="search-icon" aria-label="Search" id="desktopSearchBtn"><i class="fas fa-search"></i></a>
                    <a href="#" class="account-icon" aria-label="Account" id="desktopAccountBtn"><i class="fas fa-user"></i></a>
                </div>
            </div>
        </div>

        <?php return ob_get_clean();
    }
}
