<?php
// app/views/partials/header.php

$centerTitle = $centerTitle ?? '';
$centerI18n = $centerI18n ?? null;
$toolbarLeftHtml = $toolbarLeftHtml ?? '';
$toolbarRightHtml = $toolbarRightHtml ?? '';
$statusText = $statusText ?? '';
$statusI18n = $statusI18n ?? null;
$statusHidden = $statusHidden ?? ($statusText === '');
$statusInToolbar = $statusInToolbar ?? false;
$showQueueSummary = $showQueueSummary ?? false;
$showTopbarMenu = $showTopbarMenu ?? true;
$lang = $lang ?? 'en';
$t = $t ?? function(string $key, string $fallback = ''): string {
  return $fallback !== '' ? $fallback : $key;
};

$statusClass = $statusHidden ? 'status hidden' : 'status';
$centerAttr = $centerI18n ? ' data-i18n="' . htmlspecialchars($centerI18n, ENT_QUOTES) . '"' : '';
$statusAttr = $statusI18n ? ' data-i18n="' . htmlspecialchars($statusI18n, ENT_QUOTES) . '"' : '';
$queueActiveLabel = $t('queue.active', 'Active');
$queueErrorsLabel = $t('queue.errors', 'Errors');
$supportedLangs = ['en', 'ru', 'de'];
$rawPath = (string)($_SERVER['REQUEST_URI'] ?? '/');
$currentPath = parse_url($rawPath, PHP_URL_PATH) ?: '/';
$currentParts = array_values(array_filter(explode('/', trim($currentPath, '/'))));
if (!empty($currentParts) && in_array($currentParts[0], $supportedLangs, true)) {
  array_shift($currentParts);
}
$langPathSuffix = '/' . implode('/', $currentParts);
if ($langPathSuffix === '/') {
  $langPathSuffix = '';
}
$langNextPath = $langPathSuffix === '' ? '/' : $langPathSuffix;
$langPrefix = '/' . ($lang ?: 'en');
?>

<header class="topbar">
  <div class="left">
    <a class="product" href="https://safe-cap.com/software/scMedia" aria-label="scMedia">
      <img class="product-logo" src="/assets/icons/scmedia.png" alt="scMedia">
      <span class="sr-only">scMedia</span>
    </a>
    <span class="sse-indicator" data-sse-indicator aria-hidden="true"></span>
    <?php if ($showQueueSummary): ?>
      <div class="queue-wrap">
        <button id="queueSummary" class="status status-button" type="button" data-i18n="queue.summary">
          <span data-queue-active data-queue-active-label="<?php echo htmlspecialchars($queueActiveLabel, ENT_QUOTES); ?>">
            <?php echo htmlspecialchars($queueActiveLabel . ': 0', ENT_QUOTES); ?>
          </span>
          <span class="queue-divider is-hidden" data-queue-divider> | </span>
          <span class="is-hidden" data-queue-errors data-queue-errors-label="<?php echo htmlspecialchars($queueErrorsLabel, ENT_QUOTES); ?>">
            <?php echo htmlspecialchars($queueErrorsLabel . ': 0', ENT_QUOTES); ?>
          </span>
        </button>
        <div id="queueMenu" class="queue-menu is-hidden">
          <div class="queue-section">
            <div class="queue-title" data-i18n="queue.active"><?php echo htmlspecialchars($t('queue.active', 'Active'), ENT_QUOTES); ?></div>
            <div id="queueMenuActive"></div>
          </div>
          <div class="queue-section">
            <div class="queue-title" data-i18n="queue.finished"><?php echo htmlspecialchars($t('queue.finished', 'Finished'), ENT_QUOTES); ?></div>
            <div id="queueMenuFinished"></div>
          </div>
          <div id="queueSseStats" class="queue-section is-hidden">
            <div class="queue-title">SSE</div>
            <div class="queue-stats"></div>
          </div>
        </div>
      </div>
    <?php endif; ?>
  </div>
  <div class="center"<?php echo $centerAttr; ?>><?php echo htmlspecialchars($centerTitle, ENT_QUOTES); ?></div>
  <div class="actions">
    <?php if (!$statusInToolbar): ?>
      <div id="status" class="<?php echo $statusClass; ?>"<?php echo $statusAttr; ?>>
        <?php echo htmlspecialchars($statusText, ENT_QUOTES); ?>
      </div>
    <?php endif; ?>
    <div id="job-indicator" class="job-indicator hidden">
      <span data-i18n="job.running"><?php echo htmlspecialchars($t('job.running', 'Job running'), ENT_QUOTES); ?></span>
      <progress id="job-indicator-progress" value="0" max="100"></progress>
    </div>
    <?php if ($showTopbarMenu): ?>
      <details class="topbar-menu">
        <summary class="btn menu-button menu-avatar-button" aria-label="<?php echo htmlspecialchars($t('nav.menu', 'Menu'), ENT_QUOTES); ?>" data-i18n-aria="nav.menu">
          <img id="headerAvatar" class="menu-avatar" src="/assets/icons/scmedia.png" alt="">
        </summary>
        <nav class="menu-dropdown" aria-label="<?php echo htmlspecialchars($t('nav.menu', 'Menu'), ENT_QUOTES); ?>">
          <a class="menu-item" href="<?php echo $langPrefix; ?>/" data-i18n="nav.home"><?php echo htmlspecialchars($t('nav.home', 'Home'), ENT_QUOTES); ?></a>
          <a class="menu-item" href="<?php echo $langPrefix; ?>/settings" data-i18n="nav.settings"><?php echo htmlspecialchars($t('nav.settings', 'Settings'), ENT_QUOTES); ?></a>
          <a class="menu-item" href="<?php echo $langPrefix; ?>/account" data-i18n="nav.account"><?php echo htmlspecialchars($t('nav.account', 'Account'), ENT_QUOTES); ?></a>
          <div class="menu-divider" role="separator"></div>
          <div class="menu-inline">
            <form class="lang-form" method="get" action="/lang">
              <input type="hidden" name="next" value="<?php echo htmlspecialchars($langNextPath, ENT_QUOTES); ?>">
              <select name="lang" class="menu-select" aria-label="<?php echo htmlspecialchars($t('settings.language', 'Language'), ENT_QUOTES); ?>" onchange="this.form.submit()">
                <option value="ru"<?php echo $lang === 'ru' ? ' selected' : ''; ?>>RU</option>
                <option value="en"<?php echo $lang === 'en' ? ' selected' : ''; ?>>EN</option>
                <option value="de"<?php echo $lang === 'de' ? ' selected' : ''; ?>>DE</option>
              </select>
            </form>
            <button id="themeToggle" class="menu-item menu-theme" type="button" aria-label="<?php echo htmlspecialchars($t('actions.theme', 'Theme'), ENT_QUOTES); ?>" data-i18n-aria="actions.theme">
              <span class="menu-theme-icon" aria-hidden="true">◐</span>
            </button>
          </div>
          <div class="menu-divider" role="separator"></div>
          <button id="btnLogout" class="menu-item menu-danger" type="button" data-i18n="auth.logout"><?php echo htmlspecialchars($t('auth.logout', 'Logout'), ENT_QUOTES); ?></button>
        </nav>
      </details>
    <?php endif; ?>
  </div>
</header>

<section class="toolbar">
  <div class="toolbar-left"><?php echo $toolbarLeftHtml; ?></div>
  <div class="toolbar-right"><?php echo $toolbarRightHtml; ?></div>
</section>
