<script setup lang="ts">
import { ref, computed } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useTheme } from "@/composables/useTheme";
import { useShortcuts } from "@/composables/useShortcuts";
import { useAuthStore } from "@/stores/auth";
import { useChatStore } from "@/stores/chat";
import AppLogo from "@/components/AppLogo.vue";
import NotificationBell from "@/components/NotificationBell.vue";

const { theme, toggleTheme } = useTheme();
const { toggleShortcuts } = useShortcuts();
const authStore = useAuthStore();
const chatStore = useChatStore();
const router = useRouter();
const route = useRoute();

const isChatActive = computed(() => route.path.startsWith("/chat"));

const mobileMenuOpen = ref(false);

const statusShortLabel = computed(() => {
  if (chatStore.ollamaStatus === "unavailable") return "Offline";
  if (chatStore.ollamaStatus === "checking") return "Checking";
  if (chatStore.modelStatus === "not_found") return "No model";
  if (chatStore.modelStatus === "cold") return "Cold";
  if (chatStore.modelStatus === "loaded") return "Ready";
  return "Checking";
});

const statusLabel = computed(() => {
  if (chatStore.ollamaStatus === "unavailable") return "Ollama unavailable";
  if (chatStore.ollamaStatus === "checking") return "Checking...";
  if (chatStore.modelStatus === "not_found") return "Model not installed";
  if (chatStore.modelStatus === "cold") return "Model cold — first response may be slow";
  if (chatStore.modelStatus === "loaded") return "Model loaded · ready";
  return "Checking...";
});

const statusClass = computed(() => {
  if (chatStore.ollamaStatus === "unavailable") return "status-red";
  if (chatStore.ollamaStatus === "checking") return "status-gray";
  if (chatStore.modelStatus === "not_found") return "status-orange";
  if (chatStore.modelStatus === "cold") return "status-yellow";
  if (chatStore.modelStatus === "loaded") return "status-green";
  return "status-gray";
});

function toggleMobileMenu() {
  mobileMenuOpen.value = !mobileMenuOpen.value;
}

async function handleLogout() {
  await authStore.logout();
  mobileMenuOpen.value = false;
  router.push("/login");
}

router.afterEach(() => {
  mobileMenuOpen.value = false;
});
</script>

<template>
  <header class="app-header">
    <nav class="nav">
      <!-- Left: brand -->
      <router-link to="/" class="nav-brand">
        <AppLogo :size="34" />
        Fabled Assistant
      </router-link>

      <!-- Center: primary navigation (desktop) -->
      <div class="nav-center">
        <router-link to="/notes" class="nav-link">Notes</router-link>
        <router-link to="/projects" class="nav-link">Projects</router-link>
        <router-link to="/tasks" class="nav-link">Tasks</router-link>
        <router-link to="/chat" :class="['nav-link', { 'router-link-active': isChatActive }]">Chat</router-link>
        <router-link to="/graph" class="nav-link">Graph</router-link>
        <router-link to="/calendar" class="nav-link">Calendar</router-link>
        <router-link to="/briefing" class="nav-link">Briefing</router-link>
        <router-link to="/news" class="nav-link">News</router-link>
        <router-link to="/shared" class="nav-link">Shared</router-link>
      </div>

      <!-- Right: status + utilities + gear + user -->
      <div class="nav-right">
        <span class="status-indicator" :class="statusClass" :title="statusLabel">
          <span class="status-dot"></span>
          <span class="status-text">{{ statusShortLabel }}</span>
        </span>

        <NotificationBell />

        <button class="btn-icon" @click="toggleShortcuts" title="Keyboard shortcuts (?)">?</button>

        <button class="btn-icon" @click="toggleTheme" :title="`Switch to ${theme === 'dark' ? 'light' : 'dark'} mode`">
          {{ theme === "dark" ? "\u2600" : "\u263E" }}
        </button>

        <!-- Settings link -->
        <router-link to="/settings" class="btn-icon" aria-label="Settings" title="Settings">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <circle cx="12" cy="12" r="3"/>
            <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/>
          </svg>
        </router-link>

        <div class="user-info">
          <span class="username">{{ authStore.user?.username }}</span>
          <span v-if="authStore.isAdmin" class="admin-badge">admin</span>
          <button class="btn-logout" @click="handleLogout" title="Sign out">Sign out</button>
        </div>

        <!-- Hamburger (mobile only) -->
        <button class="hamburger" @click="toggleMobileMenu" aria-label="Toggle menu">
          <span class="hamburger-line"></span>
          <span class="hamburger-line"></span>
          <span class="hamburger-line"></span>
        </button>
      </div>
    </nav>

    <!-- Mobile dropdown -->
    <div v-if="mobileMenuOpen" class="mobile-menu">
      <router-link to="/notes" class="nav-link">Notes</router-link>
      <router-link to="/projects" class="nav-link">Projects</router-link>
      <router-link to="/tasks" class="nav-link">Tasks</router-link>
      <router-link to="/chat" :class="['nav-link', { 'router-link-active': isChatActive }]">Chat</router-link>
      <router-link to="/graph" class="nav-link">Graph</router-link>
      <router-link to="/calendar" class="nav-link">Calendar</router-link>
      <router-link to="/briefing" class="nav-link">Briefing</router-link>
      <router-link to="/news" class="nav-link">News</router-link>
      <router-link to="/shared" class="nav-link">Shared</router-link>
      <div class="mobile-divider"></div>
      <router-link to="/settings" class="nav-link">Settings</router-link>
      <div class="mobile-divider"></div>
      <div class="mobile-actions">
        <span class="status-indicator" :class="statusClass" :title="statusLabel">
          <span class="status-dot"></span>
          <span class="status-text">{{ statusShortLabel }}</span>
        </span>
        <button class="btn-icon" @click="toggleShortcuts">?</button>
        <button class="btn-icon" @click="toggleTheme">{{ theme === "dark" ? "\u2600" : "\u263E" }}</button>
      </div>
      <div class="mobile-user">
        <span class="username">{{ authStore.user?.username }}</span>
        <span v-if="authStore.isAdmin" class="admin-badge">admin</span>
        <button class="btn-logout" @click="handleLogout">Sign out</button>
      </div>
    </div>
  </header>
</template>

<style scoped>
.app-header {
  background: var(--color-bg-secondary);
  position: relative;
}
.nav {
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
}

/* Left */
.nav-brand {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-family: 'Fraunces', Georgia, serif;
  font-optical-sizing: auto;
  font-weight: 600;
  font-size: 1.15rem;
  letter-spacing: -0.01em;
  color: var(--color-primary);
  text-decoration: none;
  flex-shrink: 0;
}

/* Center — absolutely positioned so it's truly centered regardless of side widths */
.nav-center {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.1rem;
}

/* Right */
.nav-right {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  flex-shrink: 0;
}

.nav-link {
  color: var(--color-text-secondary);
  text-decoration: none;
  font-size: 0.9rem;
  padding: 0.3rem 0.6rem;
  border-radius: var(--radius-sm);
  transition: background 0.15s, color 0.15s;
}
.nav-link:hover {
  color: var(--color-primary);
  background: color-mix(in srgb, var(--color-primary) 8%, transparent);
}
.nav-link.router-link-active {
  color: var(--color-primary);
  font-weight: 600;
  box-shadow: inset 0 -2px 0 var(--color-primary);
  border-radius: 0;
}

/* Status indicator */
.status-indicator {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  cursor: default;
  padding: 0 0.25rem;
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-text {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--color-text-muted);
}
.status-green .status-dot  { background: var(--color-success, #2ecc71); }
.status-yellow .status-dot { background: var(--color-warning, #f59e0b); animation: pulse-dot 2s infinite; }
.status-orange .status-dot { background: #f97316; }
.status-red .status-dot    { background: var(--color-danger, #e74c3c); }
.status-gray .status-dot   { background: var(--color-text-muted); animation: pulse-dot 2s infinite; }
@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* Icon buttons (?, theme, gear) */
.btn-icon {
  background: none;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.25rem 0.45rem;
  cursor: pointer;
  font-size: 0.95rem;
  color: var(--color-text-muted);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-icon:hover,
.btn-icon.active {
  background: var(--color-bg-card);
  color: var(--color-text);
  border-color: var(--color-primary);
}

/* User info */
.user-info {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: 0.25rem;
  padding-left: 0.5rem;
  border-left: 1px solid var(--color-border);
}
.username {
  font-size: 0.85rem;
  color: var(--color-text-secondary);
  font-weight: 500;
}
.admin-badge {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-primary);
  background: color-mix(in srgb, var(--color-primary) 15%, transparent);
  padding: 0.1rem 0.35rem;
  border-radius: var(--radius-sm);
}
.btn-logout {
  background: none;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--color-text-secondary);
  font-family: inherit;
}
.btn-logout:hover {
  color: var(--color-danger);
  border-color: var(--color-danger);
}

/* Hamburger — mobile only */
.hamburger {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.5rem;
  flex-direction: column;
  gap: 4px;
  margin-left: 0.25rem;
}
.hamburger-line {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--color-text);
  border-radius: 1px;
}

/* Mobile menu */
.mobile-menu {
  display: flex;
  flex-direction: column;
  padding: 0.5rem 1rem 0.75rem;
  border-top: 1px solid var(--color-border);
  background: var(--color-bg-secondary);
  gap: 0.1rem;
}
.mobile-divider {
  height: 1px;
  background: var(--color-border);
  margin: 0.4rem 0;
}
.mobile-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0;
}
.mobile-user {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-top: 0.4rem;
  border-top: 1px solid var(--color-border);
  margin-top: 0.25rem;
}

@media (max-width: 768px) {
  .nav-center {
    display: none;
  }
  .status-indicator,
  .btn-icon,
  .user-info {
    display: none;
  }
  .hamburger {
    display: flex;
  }
  .mobile-menu .nav-link {
    padding: 0.5rem 0.75rem;
    min-height: 44px;
    display: flex;
    align-items: center;
  }
  .mobile-user .btn-logout {
    min-height: 36px;
  }
}
</style>
