<script>
    import { onMount } from 'svelte';
    import { auth, isAuthenticated, isAdmin, isStreamer, userColor } from '$lib/stores/auth';
    import { page } from '$app/stores';
    import '../app.css';
    
    let showDropdown = false;
    
    // Close dropdown when route changes
    $: if ($page) {
        showDropdown = false;
    }
    
    onMount(() => {
        auth.init();
        
        // Close dropdown when clicking outside
        const handleClickOutside = (event) => {
            if (!event.target.closest('.user-menu')) {
                showDropdown = false;
            }
        };
        
        document.addEventListener('click', handleClickOutside);
        return () => document.removeEventListener('click', handleClickOutside);
    });
    
    function toggleDropdown() {
        showDropdown = !showDropdown;
    }
</script>

<style>
    .nav {
        background: #111;
        border-bottom: 1px solid var(--border);
        padding: 1rem 0;
        margin-bottom: 2rem;
    }
    
    .nav-container {
        max-width: 1200px;
        margin: 0 auto;
        padding: 0 2rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    
    .nav-brand {
        font-size: 1.25rem;
        font-weight: 600;
        color: var(--white);
        text-decoration: none;
    }
    
    .nav-links {
        display: flex;
        gap: 1rem;
        align-items: center;
    }
    
    .nav-link {
        color: var(--white);
        text-decoration: none;
        transition: color 0.2s;
        padding: 0.5rem 1rem;
    }
    
    .nav-link:hover {
        color: var(--primary);
    }
    
    .user-menu {
        position: relative;
    }
    
    .user-avatar-btn {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background: var(--gray);
        border: 2px solid transparent;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--white);
        font-weight: 600;
        transition: all 0.2s;
        padding: 0;
        overflow: hidden;
        position: relative;
    }
    
    .user-avatar-btn.has-color {
        background: var(--user-color);
    }
    
    .user-avatar-btn.has-color.with-image {
        border-color: var(--user-color);
        border-width: 3px;
    }
    
    .user-avatar-btn:hover {
        transform: scale(1.05);
    }
    
    .user-avatar-btn.has-color:hover {
        box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.2);
    }
    
    .user-avatar-btn img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border: none;
    }
    
    .dropdown {
        position: absolute;
        right: 0;
        top: calc(100% + 0.5rem);
        background: #111;
        border: 1px solid var(--border);
        border-radius: 8px;
        min-width: 200px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        z-index: 1000;
        overflow: hidden;
    }
    
    .dropdown-header {
        padding: 1rem;
        border-bottom: 1px solid var(--border);
    }
    
    .dropdown-username {
        font-weight: 600;
        margin-bottom: 0.25rem;
        color: var(--white);
        text-decoration: none;
        display: flex;
        align-items: center;
        transition: color 0.2s;
    }
    
    .dropdown-username:hover {
        color: var(--primary);
    }
    
    .user-color-dot {
        width: 12px;
        height: 12px;
        border-radius: 50%;
        display: inline-block;
        margin-right: 0.5rem;
        border: 1px solid rgba(255, 255, 255, 0.2);
    }
    
    .dropdown-role {
        font-size: 0.85rem;
        color: var(--gray);
    }
    
    .dropdown-item {
        display: block;
        padding: 0.75rem 1rem;
        color: var(--white);
        text-decoration: none;
        transition: background 0.2s;
    }
    
    .dropdown-item:hover {
        background: rgba(86, 29, 94, 0.2);
    }
    
    .dropdown-divider {
        height: 1px;
        background: var(--border);
        margin: 0.5rem 0;
    }
    
    .dropdown-item.logout {
        color: var(--error);
    }
</style>

<nav class="nav">
    <div class="nav-container">
        <a href="/" class="nav-brand">Stream</a>
        
        {#if !$auth.loading}
            {#if $isAuthenticated}
                <div class="user-menu">
                    <button 
                        class="user-avatar-btn" 
                        class:has-color={$userColor}
                        class:with-image={$auth.user.avatarUrl}
                        style="--user-color: {$userColor}"
                        on:click={toggleDropdown}
                    >
                        {#if $auth.user.avatarUrl}
                            <img src={$auth.user.avatarUrl} alt={$auth.user.username} />
                        {:else}
                            {$auth.user.username.charAt(0).toUpperCase()}
                        {/if}
                    </button>
                    
                    {#if showDropdown}
                        <div class="dropdown">
                            <div class="dropdown-header">
                                <a href="/profile/{$auth.user.username}" class="dropdown-username">
                                    <span 
                                        class="user-color-dot" 
                                        style="background: {$userColor}"
                                    ></span>
                                    {$auth.user.username}
                                </a>
                                <div class="dropdown-role">
                                    {#if $isAdmin}
                                        Admin
                                    {:else if $isStreamer}
                                        Streamer
                                    {:else}
                                        User
                                    {/if}
                                </div>
                            </div>
                            
                            <a href="/settings" class="dropdown-item">
                                Settings
                            </a>
                            {#if $isStreamer}
                                <a href="/my-realms" class="dropdown-item">
                                    My Realms
                                </a>
                            {/if}
                            {#if $isAdmin}
                                <a href="/admin" class="dropdown-item">
                                    Admin
                                </a>
                            {/if}
                            
                            <div class="dropdown-divider"></div>
                            
                            <button class="dropdown-item logout" on:click={() => auth.logout()}>
                                Logout
                            </button>
                        </div>
                    {/if}
                </div>
            {:else}
                <div class="nav-links">
                    <a href="/login" class="nav-link">Login</a>
                </div>
            {/if}
        {/if}
    </div>
</nav>

<slot />