<?php
// includes/menu/header.php
//
// Top navigation for admin area.
// - Keeps the established header-button structure
// - Marks the current main area without introducing a side menu

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$adminBasePath = $adminBasePath ?? '';
$projectRootPath = $projectRootPath ?? '../';
$requestPath = (string) ($_SERVER['SCRIPT_NAME'] ?? '');

$navItems = [
    [
        'label' => 'Dashboard',
        'href' => $adminBasePath . 'dashboard.php',
        'match' => ['/admin/dashboard.php', '/admin/dashboard/'],
    ],
    [
        'label' => 'Admin',
        'href' => $adminBasePath . 'admin.php',
        'match' => ['/admin/admin.php'],
    ],
    [
        'label' => 'Empf&auml;nger',
        'href' => $adminBasePath . 'empfaenger/empfaenger.php',
        'match' => ['/admin/empfaenger/'],
    ],
    [
        'label' => 'Listen',
        'href' => $adminBasePath . 'listen.php',
        'match' => ['/admin/listen.php', '/admin/listen_', '/admin/listen'],
    ],
    [
        'label' => 'Formulare',
        'href' => $adminBasePath . 'formulare/formulare.php',
        'match' => ['/admin/formulare/'],
    ],
    [
        'label' => 'E-Mail Layouts',
        'href' => $adminBasePath . 'emails/layouts.php',
        'match' => ['/admin/emails/layouts.php', '/admin/emails/layout_', '/admin/emails/layout'],
    ],
    [
        'label' => 'E-Mail Templates',
        'href' => $adminBasePath . 'emails/templates.php',
        'match' => ['/admin/emails/templates.php', '/admin/emails/template_'],
    ],
    [
        'label' => 'Kampagnen',
        'href' => $adminBasePath . 'emails/campaigns.php',
        'match' => ['/admin/emails/campaign'],
    ],
    [
        'label' => 'Statistik',
        'href' => $adminBasePath . 'statistics/statistics.php',
        'match' => ['/admin/statistics/', '/admin/open_monitor.php', '/admin/click_monitor.php', '/admin/bounce_'],
    ],
];

$isActiveNavItem = static function (string $currentPath, array $matchers): bool {
    foreach ($matchers as $matcher) {
        if ($matcher !== '' && str_contains($currentPath, $matcher)) {
            return true;
        }
    }
    return false;
};
?>
<nav class="navbar navbar-expand-lg bg-header">
  <div class="container-fluid">
    <a class="navbar-brand" href="<?= htmlspecialchars($adminBasePath . 'dashboard.php', ENT_QUOTES, 'UTF-8') ?>">
      <img src="<?= htmlspecialchars($projectRootPath . 'templates/images/header-grafik.png', ENT_QUOTES, 'UTF-8') ?>" alt="Mailresponder Logo">
    </a>

    <button class="navbar-toggler" type="button"
            data-bs-toggle="collapse"
            data-bs-target="#topNavButtons"
            aria-controls="topNavButtons"
            aria-expanded="false"
            aria-label="Men&uuml; umschalten">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse justify-content-end" id="topNavButtons">
      <div class="top-nav-buttons d-flex flex-wrap align-items-center">
        <?php foreach ($navItems as $item): ?>
          <?php $isActive = $isActiveNavItem($requestPath, $item['match']); ?>
          <a href="<?= htmlspecialchars((string) $item['href'], ENT_QUOTES, 'UTF-8') ?>"
             class="btn btn-sm <?= $isActive ? 'btn-primary top-nav-button-active' : 'btn-secondary top-nav-button' ?>">
            <?= $item['label'] ?>
          </a>
        <?php endforeach; ?>

        <a href="<?= htmlspecialchars($adminBasePath . 'logout.php', ENT_QUOTES, 'UTF-8') ?>" class="btn btn-sm btn-outline-dark top-nav-logout">
          Logout
        </a>

        <?php if (isset($_SESSION['admin_email'])): ?>
          <span class="top-nav-user small">
            <?= htmlspecialchars((string) $_SESSION['admin_email'], ENT_QUOTES, 'UTF-8') ?>
            (<?= htmlspecialchars((string) ($_SESSION['admin_role'] ?? ''), ENT_QUOTES, 'UTF-8') ?>)
          </span>
        <?php endif; ?>
      </div>
    </div>
  </div>
</nav>
