<?php
/**
 * Shared grouped account sidebar.
 * Expected variables:
 * $menuGroups, $menuBasePath, $sidebarTitle, $sidebarHome, $sidebarContext
 */
$menuGroups = is_array($menuGroups ?? null) ? $menuGroups : [];
$menuBasePath = trim((string)($menuBasePath ?? ''), '/');
$sidebarTitle = (string)($sidebarTitle ?? 'Account');
$sidebarHome = (string)($sidebarHome ?? url('public/index.php'));
$sidebarContext = (string)($sidebarContext ?? 'Workspace');
$currentFile = basename((string)(parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?: ($_SERVER['SCRIPT_NAME'] ?? '')));

$groupIcons = [
    'Commerce Core' => 'bi-grid-1x2',
    'Catalog' => 'bi-box-seam',
    'Vendors' => 'bi-shop',
    'Customers & Members' => 'bi-people',
    'Affiliate Marketing' => 'bi-diagram-3',
    'Wallets & Finance' => 'bi-wallet2',
    'Payments' => 'bi-credit-card',
    'Delivery & Shipping' => 'bi-truck',
    'CMS, Design & SEO' => 'bi-palette',
    'Marketing & Communication' => 'bi-megaphone',
    'Reports & Analytics' => 'bi-graph-up-arrow',
    'Security & System' => 'bi-shield-lock',
    'Pro POS' => 'bi-receipt-cutoff',
    'System' => 'bi-gear',
    'Vendor Workspace' => 'bi-shop-window',
    'Customer Dashboard' => 'bi-person-circle',
    'Marketing Member' => 'bi-diagram-3',
    'Sales' => 'bi-cart-check',
    'Operations' => 'bi-sliders',
    'Reports & Settings' => 'bi-bar-chart',
    'Management' => 'bi-briefcase',
];

$itemIcon = static function (string $key, string $label): string {
    $text = strtolower($key . ' ' . $label);
    $map = [
        'dashboard' => 'bi-speedometer2', 'order' => 'bi-bag-check', 'product' => 'bi-box-seam',
        'categor' => 'bi-folder2-open', 'brand' => 'bi-patch-check', 'vendor' => 'bi-shop',
        'customer' => 'bi-person', 'member' => 'bi-person-badge', 'affiliate' => 'bi-diagram-3',
        'wallet' => 'bi-wallet2', 'withdraw' => 'bi-cash-stack', 'payment' => 'bi-credit-card',
        'invoice' => 'bi-receipt', 'delivery' => 'bi-truck', 'shipping' => 'bi-truck-flatbed',
        'courier' => 'bi-send', 'country' => 'bi-globe2', 'district' => 'bi-map', 'area' => 'bi-geo-alt',
        'page' => 'bi-file-earmark-text', 'cms' => 'bi-file-richtext', 'slider' => 'bi-images',
        'banner' => 'bi-image', 'menu' => 'bi-list-nested', 'seo' => 'bi-search', 'schema' => 'bi-braces',
        'coupon' => 'bi-ticket-perforated', 'campaign' => 'bi-megaphone', 'flash' => 'bi-lightning-charge',
        'notification' => 'bi-bell', 'email' => 'bi-envelope', 'sms' => 'bi-chat-dots', 'support' => 'bi-headset',
        'report' => 'bi-bar-chart-line', 'analytics' => 'bi-graph-up', 'stock' => 'bi-boxes',
        'role' => 'bi-person-gear', 'staff' => 'bi-people', 'security' => 'bi-shield-check',
        'login' => 'bi-door-open', 'activity' => 'bi-clock-history', 'backup' => 'bi-database-check',
        'setting' => 'bi-gear', 'theme' => 'bi-palette', 'api' => 'bi-code-slash', 'cron' => 'bi-clock',
        'pos' => 'bi-receipt-cutoff', 'register' => 'bi-display', 'shift' => 'bi-clock-history',
        'refund' => 'bi-arrow-counterclockwise', 'return' => 'bi-arrow-return-left', 'barcode' => 'bi-upc-scan',
        'profile' => 'bi-person-circle', 'address' => 'bi-geo-alt', 'wishlist' => 'bi-heart',
        'review' => 'bi-star', 'point' => 'bi-gift', 'landing' => 'bi-window', 'shop' => 'bi-shop-window',
        'team' => 'bi-diagram-3', 'rank' => 'bi-award', 'commission' => 'bi-percent', 'statement' => 'bi-file-spreadsheet',
    ];
    foreach ($map as $needle => $icon) {
        if (str_contains($text, $needle)) return $icon;
    }
    return 'bi-circle';
};
?>
<aside class="admin-sidebar account-sidebar" id="accountSidebar" aria-label="<?= e($sidebarTitle) ?> navigation">
    <div class="account-sidebar-inner">
        <div class="account-brand-wrap">
            <a class="account-brand" href="<?= e($sidebarHome) ?>">
                <img src="<?= asset('brand/logo.png') ?>" alt="<?= e(BRAND_NAME) ?>">
            </a>
            <button class="account-sidebar-close d-lg-none" type="button" data-account-sidebar-close aria-label="Close navigation"><i class="bi bi-x-lg"></i></button>
        </div>

        <div class="account-context-card">
            <span class="account-context-icon"><i class="bi <?= e($groupIcons[$sidebarTitle] ?? 'bi-grid') ?>"></i></span>
            <span><strong><?= e($sidebarTitle) ?></strong><small><?= e($sidebarContext) ?></small></span>
        </div>

        <label class="account-menu-search">
            <i class="bi bi-search"></i>
            <input type="search" placeholder="Search menu..." autocomplete="off" data-account-menu-search aria-label="Search navigation">
            <button type="button" data-account-menu-clear aria-label="Clear menu search"><i class="bi bi-x"></i></button>
        </label>

        <nav class="account-menu" data-account-menu>
            <?php foreach ($menuGroups as $group => $items):
                $groupId = 'accountGroup-' . substr(md5($sidebarTitle . '|' . $group), 0, 10);
                $groupActive = false;
                foreach ($items as $candidate) {
                    if (basename((string)($candidate[2] ?? '')) === $currentFile) { $groupActive = true; break; }
                }
            ?>
                <section class="account-menu-group <?= $groupActive ? 'is-active' : '' ?>" data-menu-group>
                    <button class="account-menu-group-toggle <?= $groupActive ? '' : 'collapsed' ?>" type="button" data-bs-toggle="collapse" data-bs-target="#<?= e($groupId) ?>" aria-expanded="<?= $groupActive ? 'true' : 'false' ?>" aria-controls="<?= e($groupId) ?>">
                        <span class="account-menu-group-label"><i class="bi <?= e($groupIcons[$group] ?? 'bi-grid') ?>"></i><span><?= e($group) ?></span></span>
                        <span class="account-menu-group-meta"><span class="account-menu-count"><?= count($items) ?></span><i class="bi bi-chevron-down account-menu-chevron"></i></span>
                    </button>
                    <div id="<?= e($groupId) ?>" class="collapse <?= $groupActive ? 'show' : '' ?> account-menu-collapse" data-menu-collapse>
                        <div class="account-menu-items">
                            <?php foreach ($items as $item):
                                $key = (string)($item[0] ?? '');
                                $label = (string)($item[1] ?? 'Module');
                                $href = (string)($item[2] ?? '#');
                                $isActive = basename($href) === $currentFile;
                                $fullUrl = $href === '#' ? '#' : url(($menuBasePath !== '' ? $menuBasePath . '/' : '') . ltrim($href, '/'));
                            ?>
                                <a class="account-menu-link <?= $isActive ? 'active' : '' ?>" href="<?= e($fullUrl) ?>" data-menu-link data-menu-text="<?= e(strtolower($group . ' ' . $label)) ?>" <?= $isActive ? 'aria-current="page"' : '' ?>>
                                    <i class="bi <?= e($itemIcon($key, $label)) ?>"></i>
                                    <span><?= e($label) ?></span>
                                    <?php if ($isActive): ?><span class="account-active-dot"></span><?php endif; ?>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </section>
            <?php endforeach; ?>
            <div class="account-menu-empty d-none" data-account-menu-empty><i class="bi bi-search"></i><span>No matching menu found</span></div>
        </nav>

        <div class="account-sidebar-footer">
            <a href="<?= url('public/index.php') ?>" target="_blank" rel="noopener"><i class="bi bi-box-arrow-up-right"></i><span>View website</span></a>
            <small>Smart Tech OMP</small>
        </div>
    </div>
</aside>
<button class="account-sidebar-overlay" type="button" data-account-sidebar-close aria-label="Close navigation"></button>
