<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}Packs Site{% endblock %}</title>
    {% load static %}
    
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    {% block extra_head %}{% endblock %}
    <link rel="stylesheet" href="{% static 'fonts/roboto.css' %}">
    <link rel="stylesheet" href="{% static 'fontawesome/css/all.min.css' %}">
    <link rel="icon" type="image/png" href="{% static 'Logo.png' %}">
</head>
<body>
    <div class="app-bar">
        <div class="app-bar-title">
            {% block app_bar_logo %}
            <img src="{% static 'Logo.png' %}" alt="Packs" height="32">
            {% endblock %}
        </div>
        <nav class="app-bar-nav">
            <span class="version-badge desktop-version">
                <i class="fas fa-tag"></i> {{ APP_ENV }}
                <span class="separator">•</span>
                <i class="fas fa-code-branch"></i> {{ GIT_COMMIT_HASH }}
                <span class="separator">•</span>
                <i class="{{ OS_ICON }}"></i> {{ OS_NAME }}
                <span class="separator">•</span>
                <i class="fas fa-clock"></i> {{ PAGE_GEN_TIME }}
            </span>

            <span class="version-badge mobile-version">
                <i class="fas fa-code-branch"></i> {{ APP_ENV|slice:":1"|upper }}
                <i class="{{ OS_ICON }}" style="margin-left: 4px;"></i>
            </span>

            <a href="{% url 'landing:home' %}">
                <i class="fas fa-home"></i><span class="nav-text"> Home</span>
            </a>
        </nav>
    </div>
    <main class="main-content">
        {% block content %}{% endblock %}
    </main>

    {% if user.is_authenticated %}
        <div class="user-bar">
            <div class="user-avatar" onclick="toggleUserMenu()">
                {% if user.userprofile.avatar %}
                    <img src="{{ user.userprofile.avatar.url }}" alt="Avatar">
                {% else %}
                    <div class="default-avatar">{{ user.username|first|upper }}</div>
                {% endif %}
            </div>
            <div class="user-menu" id="user-menu">
                {# TODO: wire edit profile page when it exists #}
                <a href="{% url 'profiles:settings' %}" class="user-edit">
                    <i class="fas fa-user-circle"></i> {{ user.username }}
                </a>
                <div class="user-info"></div>
                {# TODO: wire upload / library pages when they exist #}
                <a href="{% url 'profiles:settings' %}">
                    <i class="fas fa-cog"></i> Settings
                </a>
                <form method="post" action="{% url 'profiles:logout' %}" style="display: inline;">
                    {% csrf_token %}
                    <button type="submit" class="logout-button">
                        <i class="fas fa-sign-out-alt"></i> Logout
                    </button>
                </form>
            </div>
        </div>
        <script>
        function toggleUserMenu() {
            const menu = document.getElementById('user-menu');
            menu.style.display = menu.style.display === 'block' ? 'none' : 'block';
        }
        document.addEventListener('click', (e) => {
            if (!e.target.closest('.user-bar')) {
                document.getElementById('user-menu').style.display = 'none';
            }
        });
        function getCookie(name) {
        let cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            const cookies = document.cookie.split(';');
            for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i].trim();
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
        }
        </script>
        {% else %}
        <div class="auth-bar">
            <a href="{% url 'profiles:login' %}"><i class="fas fa-sign-in-alt"></i> Login</a> | 
            <a href="{% url 'profiles:register' %}"><i class="fas fa-user-plus"></i> Register</a>
        </div>
        {% endif %}
    {% block extra_js %}{% endblock %}
</body>
</html>