<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'

const props = usePage<SharedProps>().props
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">FlightsGoneBy</Link>

        <!-- Notification icon (always visible) -->
        <button v-if="props.auth.user" class="notif-btn" aria-label="Notifications">
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none"
                 stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
                <path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9" />
                <path d="M10.3 21a1.94 1.94 0 0 0 3.4 0" />
            </svg>
            <span class="notif-dot" />
        </button>

        <!-- Desktop nav -->
        <nav class="nav-desktop">
            <template v-if="!props.auth.user">
                <Link :href="route('login')" class="nav-link">Log In</Link>
                <Link :href="route('register')" class="nav-link nav-link--highlight">Register</Link>
            </template>
            <template v-else>
                <Link :href="route('flights.add')" class="nav-link">Add Flight</Link>
                <Link :href="route('profile.view', { user: props.auth.user.name })" class="nav-link">Profile</Link>
                <Link href="/feed" class="nav-link">Feed</Link>

                <!-- User dropdown -->
                <div class="dropdown" ref="dropdownRef">
                    <button class="nav-link dropdown-trigger" @click="dropdownOpen = !dropdownOpen">
                        {{ 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">Import from FR24</Link>
                        <div class="dropdown-divider" />
                        <button class="dropdown-item dropdown-item--danger" @click="logout">Log Out</button>
                    </div>
                </div>
            </template>
        </nav>

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

        <!-- Mobile drawer -->
        <Transition name="slide">
            <nav v-if="menuOpen" class="nav-mobile" @click="menuOpen = false">
                <template v-if="!props.auth.user">
                    <Link :href="route('login')" class="nav-link">Log In</Link>
                    <Link :href="route('register')" class="nav-link nav-link--highlight">Register</Link>
                </template>
                <template v-else>
                    <span class="nav-greeting">Welcome, {{ props.auth.user.name }}</span>
                    <Link :href="route('flights.add')" class="nav-link">Add Flight</Link>
                    <Link :href="route('profile.view', { username: props.auth.user.name })" class="nav-link">Profile</Link>
                    <Link href="/feed" class="nav-link nav-link--highlight">Feed</Link>
                    <Link :href="route('import.fr24')" class="nav-link">Import from FR24</Link>
                    <div class="dropdown-divider" />
                    <button class="nav-link nav-link--danger" @click="logout">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 {
    margin-right: auto;
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: var(--accent);
    text-decoration: none;
}

.nav-greeting {
    font-size: 0.85rem;
    font-weight: 500;
    opacity: 0.6;
    padding: 0.3em 0.25rem;
    color: var(--text);
}

/* Notification button */
.notif-btn {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text);
    border-radius: 4px;
    transition: color 0.15s ease, background 0.15s ease;
}

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

.notif-dot {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 7px;
    height: 7px;
    background: var(--accent);
    border-radius: 50%;
    border: 1.5px solid var(--bg);
}

/* Shared nav link base */
.nav-link {
    display: inline-flex;
    align-items: center;
    gap: 0.3em;
    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: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: block;
    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: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>
