<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";
import { Sun, Moon, Settings } from "lucide-vue-next";

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 isKnowledgeActive = computed(() => route.path === "/knowledge");

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" />
        <span class="brand-text">Fabled</span>
      </router-link>

      <!-- Center: primary navigation (desktop) -->
      <div class="nav-center">
        <div class="nav-pill-bar">
          <router-link to="/knowledge" class="nav-link" :class="{ 'router-link-active': isKnowledgeActive }">Knowledge</router-link>
          <router-link to="/chat" :class="['nav-link', { 'router-link-active': isChatActive }]">Chat</router-link>
          <router-link to="/journal" class="nav-link">Journal</router-link>
          <router-link to="/calendar" class="nav-link">Calendar</router-link>
          <router-link to="/projects" class="nav-link">Projects</router-link>
        </div>
      </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`">
          <Sun v-if="theme === 'dark'" :size="16" />
          <Moon v-else :size="16" />
        </button>

        <!-- Settings link -->
        <router-link to="/settings" class="btn-icon" aria-label="Settings" title="Settings">
          <Settings :size="16" />
        </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="/knowledge" class="nav-link" :class="{ 'router-link-active': isKnowledgeActive }">Knowledge</router-link>
      <router-link to="/chat" :class="['nav-link', { 'router-link-active': isChatActive }]">Chat</router-link>
      <router-link to="/journal" class="nav-link">Journal</router-link>
      <router-link to="/calendar" class="nav-link">Calendar</router-link>
      <router-link to="/projects" class="nav-link">Projects</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"><Sun v-if="theme === 'dark'" :size="16" />
          <Moon v-else :size="16" /></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: linear-gradient(180deg, var(--color-surface), var(--color-bg));
  border-bottom: 1px solid rgba(91, 74, 138, 0.18);
  position: relative;
}
.nav {
  padding: 0.6rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
}

/* Left — brand */
.nav-brand {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  text-decoration: none;
  flex-shrink: 0;
}
.brand-text {
  font-family: 'Fraunces', Georgia, serif;
  font-optical-sizing: auto;
  font-weight: 500;
  font-size: 1rem;
  letter-spacing: -0.01em;
  color: #c4b0f0;
}

/* Center — pill bar */
.nav-center {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
}
.nav-pill-bar {
  display: flex;
  align-items: center;
  gap: 2px;
  background: var(--color-primary-faint);
  border-radius: 10px;
  padding: 3px;
}

/* 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.82rem;
  padding: 0.3rem 0.75rem;
  border-radius: 8px;
  transition: background 0.15s, color 0.15s;
}
.nav-link:hover {
  color: var(--color-text);
  background: var(--color-primary-tint);
}
.nav-link.router-link-active {
  color: var(--color-primary-solid);
  font-weight: 500;
  background: rgba(91, 74, 138, 0.25);
  box-shadow: 0 0 16px rgba(91, 74, 138, 0.3);
}

/* 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 dots are indicator lights, not semantic-palette buttons —
   they want to read as vital (Moss/Warning/Error are too muted for
   a "ready" indicator). Hardcoded bright values; the rest of the
   system still uses the semantic tokens. */
.status-green .status-dot  { background: #4ade80; animation: status-pulse 2.5s ease-in-out infinite; }
.status-yellow .status-dot { background: #facc15; animation: pulse-dot 2s infinite; }
.status-orange .status-dot { background: #f97316; }
.status-red .status-dot    { background: #ef4444; }
.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; }
}
@keyframes status-pulse {
  0%, 100% { box-shadow: 0 0 4px rgba(74, 222, 128, 0.4); }
  50% { box-shadow: 0 0 10px rgba(74, 222, 128, 0.6); }
}

/* 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: 500;
  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;
    border-radius: 8px;
  }
  .mobile-menu .nav-link.router-link-active {
    background: var(--color-primary-wash);
    box-shadow: none;
  }
  .mobile-user .btn-logout {
    min-height: 36px;
  }
}
</style>
