<?php
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$uri = rtrim($uri, '/') ?: '/';

$isActive = function (string ...$paths) use ($uri): bool {
    foreach ($paths as $path) {
        if ($uri === $path || str_starts_with($uri, $path . '/')) {
            return true;
        }
    }
    return false;
};

$menuMain = $isActive('/', '/sites', '/seo');
$menuContent = $isActive('/global-topics', '/articles', '/statlink');
$menuAdmin = $isActive('/installer', '/settings', '/logs');
?>
<aside class="sidebar" id="js-sidebar">
    <div class="sidebar__brand">
        <span class="sidebar__brand-text">Back<strong>PRO</strong></span>
        <button class="sidebar__collapse-btn" id="js-sidebar-collapse" title="Zwin menu" aria-label="Zwin menu">
            <svg class="sidebar__collapse-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M15 18l-6-6 6-6"/>
            </svg>
        </button>
    </div>

    <nav class="sidebar__nav" aria-label="Menu glowne">
        <!-- Glowne -->
        <details class="sidebar__group<?= $menuMain ? ' is-active' : '' ?>"<?= $menuMain ? ' open' : '' ?>>
            <summary class="sidebar__group-toggle">
                <span class="sidebar__icon">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                        <rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/>
                    </svg>
                </span>
                <span class="sidebar__label">Glowne</span>
                <svg class="sidebar__toggle-arrow" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M6 9l6 6 6-6"/>
                </svg>
            </summary>
            <div class="sidebar__group-links">
                <a class="sidebar__sublink<?= $uri === '/' ? ' is-active' : '' ?>" href="/">Dashboard</a>
                <a class="sidebar__sublink<?= $isActive('/sites') ? ' is-active' : '' ?>" href="/sites">Strony WP</a>
                <a class="sidebar__sublink<?= $isActive('/seo') ? ' is-active' : '' ?>" href="/seo/stats">Statystyki SEO</a>
            </div>
        </details>

        <!-- Tresci -->
        <details class="sidebar__group<?= $menuContent ? ' is-active' : '' ?>"<?= $menuContent ? ' open' : '' ?>>
            <summary class="sidebar__group-toggle">
                <span class="sidebar__icon">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
                        <polyline points="14 2 14 8 20 8"/>
                        <line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/>
                    </svg>
                </span>
                <span class="sidebar__label">Tresci</span>
                <svg class="sidebar__toggle-arrow" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M6 9l6 6 6-6"/>
                </svg>
            </summary>
            <div class="sidebar__group-links">
                <a class="sidebar__sublink<?= $isActive('/global-topics') ? ' is-active' : '' ?>" href="/global-topics">Biblioteka Tematow</a>
                <a class="sidebar__sublink<?= $isActive('/articles') ? ' is-active' : '' ?>" href="/articles">Artykuly</a>
                <a class="sidebar__sublink<?= $isActive('/statlink') ? ' is-active' : '' ?>" href="/statlink">StatLink</a>
            </div>
        </details>

        <!-- Administracja -->
        <details class="sidebar__group<?= $menuAdmin ? ' is-active' : '' ?>"<?= $menuAdmin ? ' open' : '' ?>>
            <summary class="sidebar__group-toggle">
                <span class="sidebar__icon">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                        <circle cx="12" cy="12" r="3"/>
                        <path d="M12 2v2M12 20v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M2 12h2M20 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/>
                    </svg>
                </span>
                <span class="sidebar__label">Administracja</span>
                <svg class="sidebar__toggle-arrow" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M6 9l6 6 6-6"/>
                </svg>
            </summary>
            <div class="sidebar__group-links">
                <a class="sidebar__sublink<?= $isActive('/installer') ? ' is-active' : '' ?>" href="/installer">Instalator WP</a>
                <a class="sidebar__sublink<?= $isActive('/settings') && !$isActive('/settings/database') ? ' is-active' : '' ?>" href="/settings">Ustawienia API</a>
                <a class="sidebar__sublink<?= $isActive('/settings/database') ? ' is-active' : '' ?>" href="/settings/database">Baza danych</a>
                <a class="sidebar__sublink<?= $isActive('/logs') ? ' is-active' : '' ?>" href="/logs">Dziennik zdarzen</a>
            </div>
        </details>
    </nav>

    <div class="sidebar__footer">
        <div class="sidebar__user">
            <div class="sidebar__avatar">
                <?= strtoupper(substr(\App\Core\Auth::user()['username'] ?? 'U', 0, 1)) ?>
            </div>
            <div class="sidebar__user-info">
                <div class="sidebar__user-name"><?= htmlspecialchars(\App\Core\Auth::user()['username'] ?? 'Uzytkownik') ?></div>
                <a href="/logout" class="sidebar__user-logout">
                    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/>
                    </svg>
                    Wyloguj
                </a>
            </div>
        </div>
    </div>
</aside>
