<?php

require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/i18n.php';

function render_topbar(
    array $leftLinks = [],
    ?string $activeVesselName = null,
    bool $showUserMeta = false
): void {
    $user = current_user();
    ?>
    <header>
        <div class="header-top">
            <div class="header-title">
                <strong><?= e(t('portal.title', 'Yachtmemory Portal')) ?></strong>

                <?php if (!empty($leftLinks)): ?>
                    <div class="header-meta">
                        <?php foreach ($leftLinks as $index => $link): ?>
                            <?php
                            $href = (string)($link['href'] ?? '#');
                            $label = (string)($link['label'] ?? '');
                            ?>
                            <a href="<?= e($href) ?>"><?= e($label) ?></a><?= $index < count($leftLinks) - 1 ? ' — ' : '' ?>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>

                <?php if ($showUserMeta && $user): ?>
                    <div class="header-meta">
                        <?= e(t('dashboard.logged_in_as', 'Angemeldet als')) ?>
                        <?= e($user['display_name'] ?: $user['username']) ?>
                    </div>
                <?php endif; ?>
            </div>

            <div class="header-right">
                <?php if ($activeVesselName !== null && trim($activeVesselName) !== ''): ?>
                    <div class="active-vessel-indicator">
                        <span class="label"><?= e(t('dashboard.active_vessel', 'Aktives Schiff')) ?>:</span>
                        <span class="name"><?= e($activeVesselName) ?></span>
                    </div>
                <?php endif; ?>

                <div class="language-switcher">
                    <?= render_language_switcher() ?>
                </div>
		<div class="header-help-link">
		    <a href="/handbuch.php" target="_blank" rel="noopener">
		     <?= e(t('topbar.handbook', 'Handbuch')) ?>
		    </a>
		</div>
            </div>
        </div>
    </header>
    <?php
}

function render_topbar_styles(): void
{
    ?>
    <style>
        header {
            background: #1f2a36;
            color: #fff;
            padding: 16px 24px;
        }

        header a {
            color: #fff;
            text-decoration: none;
        }

        .header-top {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            gap: 16px;
            flex-wrap: wrap;
        }

        .header-title {
            min-width: 260px;
        }

        .header-title strong {
            font-size: 22px;
        }

        .header-meta {
            margin-top: 6px;
            font-size: 14px;
            opacity: 0.95;
        }

        .header-right {
            display: flex;
            flex-direction: column;
            align-items: flex-end;
            gap: 8px;
            min-width: 260px;
        }


.header-help-link {
    font-size: 0.95em;
    text-align: right;
}

.header-help-link a {
    display: inline-block;
    padding: 4px 8px;
    border: 1px solid rgba(255,255,255,0.35);
    border-radius: 6px;
}

.header-help-link a:hover {
    background: rgba(255,255,255,0.08);
}


        .active-vessel-indicator {
            text-align: right;
            font-size: 15px;
            line-height: 1.4;
            white-space: nowrap;
        }

        .active-vessel-indicator .label {
            opacity: 0.8;
            margin-right: 6px;
        }

        .active-vessel-indicator .name {
            font-weight: bold;
        }

        .language-switcher {
            font-size: 0.95em;
            text-align: right;
        }

        @media (max-width: 900px) {
            .header-right {
                align-items: flex-start;
                min-width: 0;
            }

.header-help-link {
    text-align: left;
}

            .active-vessel-indicator,
            .language-switcher {
                text-align: left;
                white-space: normal;
            }
        }
    </style>
    <?php
}
