<?php
if (!defined('ABSPATH')) exit;

$nav = $ctx['nav'] ?? [];
$page = (string)($ctx['page'] ?? '');
$dashboard_slug = (string)($ctx['dashboard_slug'] ?? 'blr-backline');
$sidebar_logo_id = \BLR\Backline\Core\SettingsPage::sidebar_logo_id();
$sidebar_logo_url = $sidebar_logo_id > 0
    ? \BLR\Backline\Core\SettingsPage::sidebar_logo_url()
    : (defined('BLR_BA_URL') ? BLR_BA_URL . 'assets/blr-ba/img/Backline.png' : '');
$show_dashboard_label = $sidebar_logo_id > 0;

/**
 * Turn a slug like "blr-backline" into "Blr Backline"
 */
$dashboard_label = ucwords(str_replace(['-', '_'], ' ', $dashboard_slug));
?>
<nav id="sidebar" class="sidebar js-sidebar">
  <div class="sidebar-content js-simplebar">
    <a class="sidebar-brand" href="<?php echo esc_url(home_url('/' . $dashboard_slug . '/')); ?>">
      <?php if ($sidebar_logo_url !== ''): ?>
        <img
          src="<?php echo esc_url($sidebar_logo_url); ?>"
          alt="<?php echo esc_attr__('Backline', 'blr-backline'); ?>"
          class="blr-ba-sidebar-logo"
        >
      <?php endif; ?>
        <span class="align-middle blr-logo-txt"><?php echo esc_html($dashboard_label); ?></span>
    </a>

    <ul class="sidebar-nav">
      <?php foreach ($nav as $item):
        $route = trim((string) ($item['route'] ?? ''), '/');
        if ($route === '') {
          $nav_path = trim((string) wp_parse_url((string) $item['url'], PHP_URL_PATH), '/');
          $route = basename($nav_path);

          if ($route === $dashboard_slug) {
            $route = 'dashboard';
          }
        }

        $active = ($page === $route || strpos($page, $route . '/') === 0) ? ' active' : '';
        if ($route === 'albums' && ($page === 'discography' || strpos($page, 'discography/') === 0)) {
          $active = ' active';
        }
      ?>
        <li class="sidebar-item<?php echo esc_attr($active); ?>">
          <a class="sidebar-link" href="<?php echo esc_url($item['url']); ?>">
            <i class="align-middle" data-feather="<?php echo esc_attr($item['icon'] ?? 'circle'); ?>"></i>
            <span class="align-middle"><?php echo esc_html($item['label']); ?></span>
          </a>
        </li>
      <?php endforeach; ?>
    </ul>
  </div>
</nav>
