<?php
// Fetch Menu Items
$menu_items = $db->getRecordSet("SELECT mi.*, p.slug as page_slug 
                                FROM cms_menu_items mi 
                                LEFT JOIN cms_pages p ON mi.page_id = p.id 
                                WHERE mi.menu_id = 1 AND mi.parent_id = 0 
                                ORDER BY mi.order_index ASC");
?>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow-sm">
    <div class="container">
        <a class="navbar-brand fw-bold" href="index.php">
            <?php echo htmlspecialchars($site_title); ?>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mainNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="mainNav">
            <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                <?php foreach ($menu_items as $item):
                    $url = ($item['link_type'] === 'page') ? $item['page_slug'] : $item['custom_url'];

                    // Fetch sub-items
                    $sub_items = $db->getRecordSet("SELECT mi.*, p.slug as page_slug 
                                                   FROM cms_menu_items mi 
                                                   LEFT JOIN cms_pages p ON mi.page_id = p.id 
                                                   WHERE mi.parent_id = :pid 
                                                   ORDER BY mi.order_index ASC", ['pid' => $item['id']]);
                    ?>
                    <?php if (empty($sub_items)): ?>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo htmlspecialchars($url); ?>">
                                <?php echo htmlspecialchars($item['label']); ?>
                            </a>
                        </li>
                    <?php else: ?>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
                                aria-expanded="false">
                                <?php echo htmlspecialchars($item['label']); ?>
                            </a>
                            <ul class="dropdown-menu">
                                <?php foreach ($sub_items as $sub):
                                    $sub_url = ($sub['link_type'] === 'page') ? $sub['page_slug'] : $sub['custom_url'];
                                    ?>
                                    <li><a class="dropdown-item"
                                            href="<?php echo htmlspecialchars($sub_url); ?>"><?php echo htmlspecialchars($sub['label']); ?></a>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </li>
                    <?php endif; ?>
                <?php endforeach; ?>
                <!-- If no menu exists, show default links -->
                <?php if (empty($menu_items)): ?>
                    <li class="nav-item"><a class="nav-link" href="home">ہوم (Home)</a></li>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</nav>