<?php
$app = app();
$isLoggedIn = isset($_SESSION['user_id']);
$displayName = 'Profil';
$profileInitial = 'P';
$showAdminLink = false;

if ($isLoggedIn) {
    try {
        $pdo = $app->pdo();
        if ($pdo) {
            $profileSettings = new \App\ProfileSettings($pdo);
            $profileSettings->ensureSchema();
            $stmt = $pdo->prepare('SELECT display_name, first_name, avatar_preset, avatar_lorelei_eyes_variant, avatar_lorelei_eyebrows_variant, avatar_lorelei_mouth_variant, avatar_lorelei_glasses_variant, avatar_lorelei_hair_variant, avatar_lorelei_beard_variant, avatar_lorelei_earrings_variant FROM user_profiles WHERE user_id = :id LIMIT 1');
            $stmt->execute(['id' => (int)$_SESSION['user_id']]);
            $profileRow = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
            $displayName = trim((string)($profileRow['display_name'] ?? '')) ?: trim((string)($profileRow['first_name'] ?? '')) ?: 'Profil';

            $communityCfg = dirname(__DIR__, 2) . '/config/community.php';
            if (file_exists($communityCfg)) {
                $communityConfig = require $communityCfg;
                $communityAccess = new \App\CommunityAccess($pdo, $communityConfig);
                $showAdminLink = $communityAccess->canModerateForum((int)$_SESSION['user_id']);
            }
        }
    } catch (\Throwable) {
        $displayName = 'Profil';
        $profileInitial = 'P';
        $showAdminLink = false;
    }
}
?>
<header class="site-header">
  <div class="container nav-row">
    <div class="brand">
      <img data-logo-img src="/assets/bilder/logo_male.png" alt="Papa-Kind-Treff Logo" class="brand__logo">
      <div class="brand__text">
        <span class="brand__name">Papa-Kind-Treff</span>
        <span class="brand__tag">Väter vernetzen</span>
      </div>
    </div>
    <nav class="nav-links" aria-label="Hauptmenü">
      <a href="/">Home</a>
      <a href="/search">Suche</a>
      <a href="/community">Community</a>
    </nav>
    <div class="nav-actions">
      <?php if ($isLoggedIn): ?>
        <div class="profile-menu" data-profile-menu>
          <button class="profile-menu__trigger" type="button" aria-haspopup="menu" aria-expanded="false" data-profile-menu-trigger>
            <span class="profile-menu__avatar" aria-hidden="true"><?= \App\Avatar\Lorelei::render($profileRow ?? [], $displayName, 'nav') ?></span>
            <span class="profile-menu__name"><?= htmlspecialchars($displayName, ENT_QUOTES) ?></span>
          </button>
          <div class="profile-menu__dropdown" role="menu">
            <a href="/dashboard?section=profile" role="menuitem">Profil</a>
            <a href="/dashboard?section=children" role="menuitem">Kinder</a>
            <a href="/dashboard?section=events" role="menuitem">Termine</a>
            <a href="/dashboard?section=community" role="menuitem">Community</a>
            <a href="/dashboard?section=settings" role="menuitem">Einstellungen</a>
            <?php if ($showAdminLink): ?>
              <a href="/community-admin" role="menuitem">Admin</a>
            <?php endif; ?>
            <a href="/logout" role="menuitem">Abmelden</a>
          </div>
        </div>
      <?php else: ?>
        <a class="btn ghost" href="/login">Anmelden</a>
        <a class="btn" href="/register">Kostenlos registrieren</a>
      <?php endif; ?>
      <button class="menu-toggle" aria-label="Menü öffnen">☰</button>
    </div>
  </div>
  <div class="mobile-menu" id="mobileMenu">
    <a href="/">Home</a>
    <a href="/search">Suche</a>
    <a href="/community">Community</a>
    <?php if ($isLoggedIn): ?>
      <a class="btn ghost" href="/dashboard?section=profile">Profil</a>
      <a class="btn ghost" href="/dashboard?section=children">Kinder</a>
      <a class="btn ghost" href="/dashboard?section=events">Termine</a>
      <a class="btn ghost" href="/dashboard?section=community">Community</a>
      <a class="btn ghost" href="/dashboard?section=settings">Einstellungen</a>
      <?php if ($showAdminLink): ?>
        <a class="btn ghost" href="/community-admin">Admin</a>
      <?php endif; ?>
      <a class="btn block" href="/logout">Abmelden</a>
    <?php else: ?>
      <a class="btn ghost" href="/login">Anmelden</a>
      <a class="btn block" href="/register">Kostenlos registrieren</a>
    <?php endif; ?>
  </div>
</header>
<div id="headerSentinel" class="header-sentinel" aria-hidden="true"></div>
