<script setup lang="ts">
import { Link, useForm } from "@inertiajs/vue3";
import { usePage } from '@inertiajs/vue3'
import type { SharedProps } from '@/Types/types'
import { ref, onMounted, onUnmounted } from 'vue'
import NotificationMenu from "@/Components/FlightsGoneBy/NotificationMenu.vue";
import Logo from "@/Components/FlightsGoneBy/Logo.vue";

const page = usePage<SharedProps>()
const menuOpen = ref(false)
const dropdownOpen = ref(false)
const dropdownRef = ref<HTMLElement | null>(null)

const logoutForm = useForm({})
const logout = () => logoutForm.post(route('logout'))

const handleClickOutside = (e: MouseEvent) => {
    if (dropdownRef.value && !dropdownRef.value.contains(e.target as Node)) {
        dropdownOpen.value = false
    }
}

onMounted(() => document.addEventListener('click', handleClickOutside))
onUnmounted(() => document.removeEventListener('click', handleClickOutside))
</script>

<template>
    <header class="glass">
        <Link href="/" class="brand">
            <Logo variant="color" />
        </Link>

        <NotificationMenu v-if="page.props.auth?.user" :unread-count="page.props.unread_notification_count" />

        <!-- Desktop nav -->
        <nav class="nav-desktop">
            <template v-if="!page.props.auth?.user">
                <Link :href="route('login')" class="nav-link">
                    <i class="mdi mdi-login" aria-hidden="true" />
                    Log In
                </Link>
                <Link :href="route('register')" class="nav-link nav-link--highlight">
                    <i class="mdi mdi-account-plus" aria-hidden="true" />
                    Register
                </Link>
            </template>
            <template v-else>
                <Link :href="route('flights.add')" class="nav-link">
                    <i class="mdi mdi-airplane-plus" aria-hidden="true" />
                    Add Flight
                </Link>
                <Link :href="route('profile.view', { user: page.props.auth?.user.name })" class="nav-link">
                    <i class="mdi mdi-account" aria-hidden="true" />
                    Profile
                </Link>
                <Link :href="route('feed')" class="nav-link">
                    <i class="mdi mdi-timeline-text" aria-hidden="true" />
                    Feed
                </Link>
                <Link v-if="page.props.auth.roles.includes('admin')" :href="route('admin.dashboard')" class="nav-link">
                    <i class="mdi mdi-shield-account" aria-hidden="true" />
                    Admin
                </Link>

                <div class="dropdown" ref="dropdownRef">
                    <button class="nav-link dropdown-trigger" @click.stop="dropdownOpen = !dropdownOpen">
                        <i class="mdi mdi-account-circle" aria-hidden="true" />
                        {{ page.props.auth.user.name }}
                        <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none"
                             stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <path d="m6 9 6 6 6-6"/>
                        </svg>
                    </button>
                    <div v-if="dropdownOpen" class="dropdown-menu">
                        <Link :href="route('import.fr24')" class="dropdown-item">
                            <i class="mdi mdi-cloud-download" aria-hidden="true" />
                            Import from FR24
                        </Link>
                        <Link :href="route('user.settings')" class="dropdown-item">
                            <i class="mdi mdi-cog" aria-hidden="true" />
                            Settings
                        </Link>
                        <div class="dropdown-divider" />
                        <Link :href="route('support')" class="dropdown-item">
                            <i class="mdi mdi-lifebuoy" aria-hidden="true" />
                            Support
                        </Link>
                        <div class="dropdown-divider" />
                        <button class="dropdown-item dropdown-item--danger" @click="logout">
                            <i class="mdi mdi-logout" aria-hidden="true" />
                            Log Out
                        </button>
                    </div>
                </div>
            </template>
        </nav>

        <!-- Hamburger (mobile only) -->
        <button class="hamburger" :class="{ open: menuOpen }" @click.stop="menuOpen = !menuOpen" aria-label="Toggle menu">
            <span /><span /><span />
        </button>

        <!-- Mobile drawer -->
        <Transition name="slide">
            <nav v-if="menuOpen" class="nav-mobile" @click.stop>
                <template v-if="!page.props.auth?.user">
                    <Link :href="route('login')" class="nav-link" @click="menuOpen = false">
                        <i class="mdi mdi-login" aria-hidden="true" />
                        Log In
                    </Link>
                    <Link :href="route('register')" class="nav-link nav-link--highlight" @click="menuOpen = false">
                        <i class="mdi mdi-account-plus" aria-hidden="true" />
                        Register
                    </Link>
                </template>
                <template v-else>
                    <span class="nav-greeting">Welcome, {{ page.props.auth.user.name }}</span>
                    <Link :href="route('flights.add')" class="nav-link" @click="menuOpen = false">
                        <i class="mdi mdi-airplane-plus" aria-hidden="true" />
                        Add Flight
                    </Link>
                    <Link :href="route('profile.view', { user: page.props.auth.user.name })" class="nav-link" @click="menuOpen = false">
                        <i class="mdi mdi-account" aria-hidden="true" />
                        Profile
                    </Link>
                    <Link :href="route('feed')" class="nav-link nav-link" @click="menuOpen = false">
                        <i class="mdi mdi-timeline-text" aria-hidden="true" />
                        Feed
                    </Link>
                    <Link :href="route('import.fr24')" class="nav-link" @click="menuOpen = false">
                        <i class="mdi mdi-cloud-download" aria-hidden="true" />
                        Import from FR24
                    </Link>
                    <Link :href="route('user.settings')" class="nav-link" @click="menuOpen = false">
                        <i class="mdi mdi-cog" aria-hidden="true" />
                        Settings
                    </Link>
                    <div class="dropdown-divider" />
                    <Link :href="route('support')" class="dropdown-item">
                        <i class="mdi mdi-lifebuoy" aria-hidden="true" />
                        Support
                    </Link>
                    <div class="dropdown-divider" />
                    <button class="nav-link nav-link--danger" @click="logout">
                        <i class="mdi mdi-logout" aria-hidden="true" />
                        Log Out
                    </button>
                </template>
            </nav>
        </Transition>
    </header>
</template>

<style scoped>
header {
    display: flex;
    align-items: center;
    flex: 0 0 5dvh;
    min-height: 48px;
    padding: 0 1.25rem;
    position: relative;
    z-index: 10;
    gap: 0.5rem;
}

.brand {
    display: flex;
    align-items: center;
    height: 100%;
    margin-right: auto;
    text-decoration: none;
}
.nav-greeting {
    font-size: 0.85rem;
    font-weight: 500;
    opacity: 0.6;
    padding: 0.3em 0.25rem;
    color: var(--text);
}


/* Shared nav link base */
.nav-link {
    display: flex;
    align-items: center;
    gap: 0.4em;
    height: 100%;
    padding: 0.3em 0.75em;
    font-size: 0.875rem;
    font-weight: 500;
    letter-spacing: 0.04em;
    text-decoration: none;
    color: var(--text);
    border-radius: 4px;
    border: none;
    background: none;
    cursor: pointer;
    transition: color 0.15s ease, background 0.15s ease;
}

.nav-link .mdi {
    font-size: 1rem;
    line-height: 1;
    opacity: 0.8;
}

.nav-link:visited {
    color: var(--text);
}

.nav-link:hover {
    color: var(--accent);
    background: rgba(56, 189, 248, 0.07);
}

.nav-link--highlight {
    border: 1px solid rgba(56, 189, 248, 0.35);
    color: var(--accent);
}

.nav-link--highlight:visited {
    color: var(--accent);
}

.nav-link--highlight:hover {
    background: rgba(56, 189, 248, 0.12);
}

.nav-link--danger {
    width: 100%;
    color: #f87171;
}

.nav-link--danger:hover {
    color: #f87171;
    background: rgba(248, 113, 113, 0.08);
}

/* Dropdown */
.dropdown {
    position: relative;
}

.dropdown-trigger {
    font-family: inherit;
}

.dropdown-menu {
    position: absolute;
    top: calc(100% + 0.4rem);
    right: 0;
    min-width: 180px;
    background: var(--bg);
    border: 1px solid rgba(56, 189, 248, 0.12);
    border-radius: 6px;
    padding: 0.35rem;
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

.dropdown-item {
    display: flex;
    align-items: center;
    gap: 0.5em;
    width: 100%;
    padding: 0.4em 0.75em;
    font-size: 0.875rem;
    font-weight: 500;
    letter-spacing: 0.03em;
    text-decoration: none;
    color: var(--text);
    border-radius: 4px;
    border: none;
    background: none;
    cursor: pointer;
    text-align: left;
    transition: color 0.15s ease, background 0.15s ease;
}

.dropdown-item .mdi {
    font-size: 1rem;
    line-height: 1;
    opacity: 0.8;
}

.dropdown-item:hover {
    color: var(--accent);
    background: rgba(56, 189, 248, 0.07);
}

.dropdown-item--danger {
    color: #f87171;
}

.dropdown-item--danger:hover {
    color: #f87171;
    background: rgba(248, 113, 113, 0.08);
}

.dropdown-divider {
    height: 1px;
    background: rgba(56, 189, 248, 0.1);
    margin: 0.25rem 0;
}

/* Desktop nav */
.nav-desktop {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Hamburger */
.hamburger {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 36px;
    height: 36px;
    padding: 6px;
    background: none;
    border: none;
    cursor: pointer;
}

.hamburger span {
    display: block;
    width: 100%;
    height: 1.5px;
    background: var(--text);
    border-radius: 2px;
    transition: transform 0.2s ease, opacity 0.2s ease;
    transform-origin: center;
}

.hamburger.open span:nth-child(1) { transform: translateY(6.5px) rotate(45deg); }
.hamburger.open span:nth-child(2) { opacity: 0; }
.hamburger.open span:nth-child(3) { transform: translateY(-6.5px) rotate(-45deg); }

/* Mobile nav drawer */
.nav-mobile {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.75rem 1rem;
    background: var(--bg);
    border-bottom: 1px solid rgba(56, 189, 248, 0.1);
    z-index: 20;
}

.slide-enter-from,
.slide-leave-to {
    opacity: 0;
    transform: translateY(-6px);
}

.slide-enter-active,
.slide-leave-active {
    transition: opacity 0.15s ease, transform 0.15s ease;
}

@media (max-width: 600px) {
    .nav-desktop { display: none; }
    .hamburger { display: flex; }
}
</style>
