<?php

use MintyPHP\Session;

$user = $_SESSION['user'] ?? [];
$accountUrl = accountUrl();
$userUuid = trim((string) ($user['uuid'] ?? ''));
$accountEditUrl = $userUuid !== '' ? lurl("admin/users/edit/{$userUuid}") : $accountUrl;
$themes = appThemes();
$theme = currentTheme();
$themeIcon = str_starts_with($theme, 'dark') ? 'bi-moon-stars-fill' : 'bi-sun-fill';
$csrfKey = Session::$csrfSessionKey;
$csrfToken = $_SESSION[$csrfKey] ?? '';

// Tenant switcher data
$currentTenant = $_SESSION['current_tenant'] ?? null;
$currentTenantId = $currentTenant['id'] ?? null;
$availableTenants = $_SESSION['available_tenants'] ?? [];

?>
<header class="app-header">
  <nav>
    <ul>
      <li>
        <a id="global-back" data-tooltip-pos="bottom" data-tooltip="<?php e(t('Back')); ?> (Alt+←)" title="<?php e(t('Back')); ?> (Alt+←)" href="admin"><i class="bi bi-arrow-left"></i></a>
      </li>
      <li>
        <a id="global-forward" data-tooltip-pos="bottom" data-tooltip="<?php e(t('Forward')); ?> (Alt+→)" title="<?php e(t('Forward')); ?> (Alt+→)" href="admin"><i class="bi bi-arrow-right"></i></a>
      </li>
      <li id="async-messages"></li>
    </ul>
    <ul class="nav-list">
      <li data-tooltip="<?php e(t("Toggle Sidebar"));?>" data-tooltip-pos="bottom">
        <button id="toggle-aside" data-sidebar-toggle data-sidebar-action="visibility"><i class="bi bi-layout-sidebar-inset"></i></button>
      </li>
      <li data-tooltip="<?php e(t("Toggle Detail Sidebar"));?>" data-tooltip-pos="bottom">
        <button id="toggle-main-content-aside"><i class="bi bi-layout-sidebar-inset-reverse"></i></button>
      </li>
      <?php if (allowUserTheme()): ?>
        <li data-tooltip="<?php e(t('Theme')); ?>" data-tooltip-pos="bottom">
          <details class="dropdown"
            data-theme-menu
            data-theme-url="admin/users/theme"
            data-csrf-key="<?php e($csrfKey); ?>"
            data-csrf-token="<?php e($csrfToken); ?>">
            <summary aria-label="<?php e(t('Toggle theme')); ?>">
              <i class="bi <?php e($themeIcon); ?>" data-theme-icon></i>
            </summary>
            <ul dir="rtl">
              <?php foreach ($themes as $key => $label): ?>
                <?php $isActiveTheme = $key === $theme; ?>
                <li>
                  <a href="#" data-theme-option data-theme-value="<?php e($key); ?>"
                    class="<?php e($isActiveTheme ? 'active' : ''); ?>"
                    <?php echo $isActiveTheme ? 'aria-current="true"' : ''; ?>>
                    <?php e(t($label)); ?>
                  </a>
                </li>
              <?php endforeach; ?>
            </ul>
          </details>
        </li>
      <?php endif; ?>
      <li data-tooltip="<?php e(t('High contrast')); ?>" data-tooltip-pos="bottom">
        <button type="button" data-contrast-toggle aria-label="<?php e(t('Toggle contrast')); ?>">
          <i class="bi bi-circle-half"></i>
        </button>
      </li>
      <?php if ($currentTenant && count($availableTenants) > 1): ?>
        <li data-tooltip="<?php e(t('Switch tenant')); ?>" data-tooltip-pos="bottom">
          <details class="dropdown">
            <summary><i class="bi bi-buildings"></i></summary>
            <ul dir="rtl">
              <?php foreach ($availableTenants as $tenant): ?>
                <?php $isActive = ($tenant['id'] ?? 0) === $currentTenantId; ?>
                <li>
                  <a href="#"
                     class="<?php echo $isActive ? 'active' : ''; ?>"
                     <?php if (!$isActive): ?>
                       data-switch-tenant="<?php e($tenant['id'] ?? ''); ?>"
                       data-csrf-key="<?php e($csrfKey); ?>"
                       data-csrf-token="<?php e($csrfToken); ?>"
                       data-error-message="<?php e(t('Failed to switch tenant')); ?>"
                     <?php endif; ?>>
                    <?php e($tenant['description'] ?? ''); ?>
                    <?php if ($isActive): ?>
                      <i class="bi bi-check"></i>
                    <?php endif; ?>
                  </a>
                </li>
              <?php endforeach; ?>
            </ul>
          </details>
        </li>
      <?php endif; ?>
      <li>
        <?php
        $variant = 'dropdown';
        require __DIR__ . '/app-language-switcher.phtml';
        unset($variant);
        ?>
      </li>
      <li>
        <details class="dropdown">
          <summary><i class="bi bi-person-fill"></i></summary>
          <ul dir="rtl">
            <li><a href="<?php e($accountEditUrl); ?>">
                <?php e(t('Account')); ?>
              </a>
            </li>
            <li><a href="<?php e(lurl('help/hotkeys')); ?>">
                <?php e(t('Keyboard shortcuts')); ?>
              </a>
            </li>
            <li><a href="logout">
                <?php e(t('Logout')); ?>
              </a>
            </li>
          </ul>
        </details>
      </li>
    </ul>
  </nav>
</header>
