<?php
$user = $_SESSION['user'] ?? null;
$role = $user['role_name'] ?? '';
$currentPage = basename($_SERVER['PHP_SELF'] ?? '');

$navSections = [
    'General' => [
        ['label' => 'Dashboard', 'href' => 'dashboard.php', 'roles' => ['admin', 'staff'], 'meta' => 'Home'],
        ['label' => 'Staff Dashboard', 'href' => 'staff_dashboard.php', 'roles' => ['staff'], 'meta' => 'Daily'],
        ['label' => 'Driver App', 'href' => 'driver_app.php', 'roles' => ['driver'], 'meta' => 'Mobile'],
    ],
    'Inventory' => [
        ['label' => 'Products', 'href' => 'products.php', 'roles' => ['admin', 'staff'], 'meta' => 'Catalog'],
        ['label' => 'Stock In', 'href' => 'stock_in.php', 'roles' => ['admin', 'staff'], 'meta' => 'Receive'],
        ['label' => 'Stock Out', 'href' => 'stock_out.php', 'roles' => ['admin', 'staff'], 'meta' => 'Release'],
        ['label' => 'Adjust Stock', 'href' => 'adjust_stock.php', 'roles' => ['admin', 'staff'], 'meta' => 'Correction'],
        ['label' => 'Stock Summary', 'href' => 'stock_summary.php', 'roles' => ['admin', 'staff'], 'meta' => 'Levels'],
        ['label' => 'QR / Labels', 'href' => 'qr_sheets.php', 'roles' => ['admin'], 'meta' => 'Print'],
    ],
    'Delivery' => [
        ['label' => 'Deliveries', 'href' => 'deliveries.php', 'roles' => ['admin', 'staff'], 'meta' => 'Orders'],
        ['label' => 'Live Map', 'href' => 'live_map.php', 'roles' => ['admin', 'staff'], 'meta' => 'Tracking'],
        ['label' => 'POD Submit', 'href' => 'driver_pod.php', 'roles' => ['driver'], 'meta' => 'Proof'],
    ],
    'Management' => [
        ['label' => 'Suppliers', 'href' => 'suppliers.php', 'roles' => ['admin', 'staff'], 'meta' => 'Vendors'],
        ['label' => 'Customers', 'href' => 'customers.php', 'roles' => ['admin'], 'meta' => 'Clients'],
        ['label' => 'Product Categories', 'href' => 'product_categories.php', 'roles' => ['admin', 'staff'], 'meta' => 'Groups'],
        ['label' => 'Reports', 'href' => 'reports.php', 'roles' => ['admin', 'staff'], 'meta' => 'Exports'],
        ['label' => 'Users', 'href' => 'users.php', 'roles' => ['admin'], 'meta' => 'Access'],
        ['label' => 'Audit Logs', 'href' => 'audit_logs.php', 'roles' => ['admin'], 'meta' => 'History'],
    ],
];
?>
<aside class="col-12 col-lg-2 app-sidebar">
  <div class="app-sidebar-panel">
    <?php foreach ($navSections as $sectionLabel => $items): ?>
      <?php
      $allowedItems = array_values(array_filter($items, static function (array $item) use ($role): bool {
          return in_array($role, $item['roles'], true);
      }));
      if (!$allowedItems) {
          continue;
      }
      ?>
      <div class="app-nav-section"><?= htmlspecialchars($sectionLabel, ENT_QUOTES, 'UTF-8') ?></div>
      <?php foreach ($allowedItems as $item): ?>
        <?php $isActive = $currentPage === $item['href']; ?>
        <a href="<?= htmlspecialchars($item['href'], ENT_QUOTES, 'UTF-8') ?>" class="app-nav-link<?= $isActive ? ' active' : '' ?>">
          <span><?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?></span>
          <span class="app-nav-meta"><?= htmlspecialchars($item['meta'], ENT_QUOTES, 'UTF-8') ?></span>
        </a>
      <?php endforeach; ?>
    <?php endforeach; ?>
  </div>
</aside>
