<script setup lang="ts">
/**
 * Sidebar — kompakte Icon-Rail (V2-Shell, alle Seiten)
 *
 * Collapsed 68px, expandiert bei Hover auf 232px als Overlay
 * (kein Layout-Shift im Content). Ersetzt Sidebar + Topbar.
 * Mobile: als Drawer über mobileOpen/close.
 */
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '../../stores/auth'
import { useNotificationStore } from '../../stores/notifications'
import { useLiveSyncStore } from '../../stores/liveSync'
import { railNav, railFooterNav, svg } from '../../composables/icons'
import { initials } from '../../utils/format'

defineProps<{
  mobileOpen?: boolean
}>()

const emit = defineEmits<{
  close: []
}>()

const auth = useAuthStore()
const route = useRoute()
const router = useRouter()
const notificationStore = useNotificationStore()
const liveSync = useLiveSyncStore()

const ownerInitials = computed(() =>
  auth.user?.displayName ? initials(auth.user.displayName) : 'OW'
)

function isActive(itemRoute?: string): boolean {
  if (!itemRoute) return false
  if (route.path === itemRoute) return true
  // Detailrouten (/tasks/:id, /agents/:id) markieren den Hauptpunkt
  return route.path.startsWith(itemRoute + '/')
}

function navigate(itemRoute?: string) {
  if (!itemRoute) return
  emit('close')
  router.push(itemRoute)
}

async function logout() {
  await auth.logout()
  await router.replace('/login')
}

const statusLabel = computed(() => {
  if (liveSync.connected) return 'Live'
  if (liveSync.connecting) return 'Verbinde…'
  return 'Polling'
})
</script>

<template>
  <aside :class="['rail', { open: mobileOpen }]">
    <!-- Brand -->
    <button class="rail-brand" @click="navigate('/dashboard')">
      <span class="brand-mark" v-html="svg('command')"></span>
      <span class="rail-label brand-label">NEXUS</span>
    </button>

    <!-- Navigation -->
    <nav class="rail-nav v2-scroll">
      <button
        v-for="item in railNav"
        :key="item.route"
        :class="['rail-item', { active: isActive(item.route) }]"
        :title="item.label"
        @click="navigate(item.route)"
      >
        <span class="rail-icon" v-html="svg(item.icon)"></span>
        <span
          v-if="item.route === '/notifications' && notificationStore.unreadCount > 0"
          class="rail-dot"
        ></span>
        <span class="rail-label">{{ item.label }}</span>
        <span
          v-if="item.route === '/notifications' && notificationStore.unreadCount > 0"
          class="rail-count"
        >{{ notificationStore.unreadCount }}</span>
      </button>
    </nav>

    <!-- Footer -->
    <div class="rail-foot">
      <div class="rail-item static" :title="statusLabel">
        <span class="rail-icon">
          <span :class="['status-dot', liveSync.connected ? 'on' : 'off']"></span>
        </span>
        <span class="rail-label dim">{{ statusLabel }}</span>
      </div>

      <button
        v-for="item in railFooterNav"
        :key="item.route"
        :class="['rail-item', { active: isActive(item.route) }]"
        :title="item.label"
        @click="navigate(item.route)"
      >
        <span class="rail-icon" v-html="svg(item.icon)"></span>
        <span class="rail-label">{{ item.label }}</span>
      </button>

      <div class="rail-owner">
        <span class="avatar">{{ ownerInitials }}</span>
        <span class="rail-label owner-label">
          <span class="owner-name">{{ auth.user?.displayName ?? 'Owner' }}</span>
          <span class="owner-role">{{ auth.user?.role ?? 'Owner' }}</span>
        </span>
        <button class="logout-btn rail-label" title="Abmelden" @click="logout" v-html="svg('logout')"></button>
      </div>
    </div>
  </aside>
</template>

<style scoped>
.rail {
  position: absolute;
  inset: 0 auto 0 0;
  width: 68px;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, rgba(14, 12, 32, 0.92), rgba(8, 6, 20, 0.92));
  border-right: 1px solid var(--line);
  backdrop-filter: blur(14px);
  overflow: hidden;
  transition: width .18s ease, box-shadow .18s ease;
  z-index: 100;
}

.rail:hover {
  width: 232px;
  box-shadow: 24px 0 60px -30px rgba(0, 0, 0, .8);
}

/* Labels: unsichtbar bis die Rail expandiert */
.rail-label {
  opacity: 0;
  white-space: nowrap;
  transition: opacity .14s ease .04s;
  font-size: 13px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rail:hover .rail-label,
.rail.open .rail-label {
  opacity: 1;
}

/* ── Brand ── */
.rail-brand {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 15px;
  border: none;
  background: transparent;
  cursor: pointer;
}

.brand-mark {
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  background: var(--grad);
  box-shadow: var(--glow-purple);
}

.brand-mark :deep(svg) {
  width: 20px;
  height: 20px;
  color: #fff;
}

.brand-label {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: .14em;
  color: var(--tx);
}

/* ── Nav ── */
.rail-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 6px 12px;
  overflow-y: auto;
  overflow-x: hidden;
}

.rail-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 13px;
  height: 42px;
  padding: 0 13px;
  flex: 0 0 auto;
  border: none;
  border-radius: 11px;
  background: transparent;
  color: var(--tx-2);
  font-family: 'Manrope', sans-serif;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.rail-item:not(.static):hover {
  background: rgba(124, 108, 255, .08);
  color: var(--tx);
}

.rail-item.active {
  color: #fff;
  background: linear-gradient(90deg, rgba(124, 108, 255, .22), rgba(124, 108, 255, .04));
  box-shadow: inset 0 0 0 1px rgba(124, 108, 255, .25);
}

.rail-item.static {
  cursor: default;
}

.rail-icon {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  display: grid;
  place-items: center;
  opacity: .9;
}

.rail-icon :deep(svg) {
  width: 18px;
  height: 18px;
}

.rail-item .rail-label {
  flex: 1;
}

/* Ungelesen-Punkt am Icon (collapsed sichtbar) */
.rail-dot {
  position: absolute;
  left: 24px;
  top: 9px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--st-block);
  box-shadow: 0 0 8px rgba(251, 113, 133, .8);
}

.rail-count {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10.5px;
  font-weight: 600;
  padding: 1px 8px;
  border-radius: 20px;
  background: rgba(251, 113, 133, .16);
  border: 1px solid rgba(251, 113, 133, .35);
  color: var(--st-block);
}

/* ── Footer ── */
.rail-foot {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 6px 12px 10px;
  border-top: 1px solid var(--line);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.status-dot.on {
  background: var(--st-work);
  animation: pulse-work 1.8s infinite;
}

.status-dot.off {
  background: var(--st-idle);
}

.rail-label.dim {
  color: var(--tx-3);
  font-size: 12px;
}

.rail-owner {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 7px 5px 2px;
}

.avatar {
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  border-radius: 10px;
  background: var(--grad-soft);
  border: 1px solid var(--line-2);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 12px;
  color: var(--tx);
}

.owner-label {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.owner-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--tx);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.owner-role {
  font-size: 10px;
  color: var(--tx-3);
  text-transform: capitalize;
}

.logout-btn {
  border: none;
  background: transparent;
  color: var(--tx-3);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: grid;
  place-items: center;
}

.logout-btn:hover {
  color: var(--st-block);
  background: rgba(251, 113, 133, .1);
}

.logout-btn :deep(svg) {
  width: 16px;
  height: 16px;
}

/* ── Mobile: Drawer ── */
@media (max-width: 767px) {
  .rail {
    position: fixed;
    width: 232px;
    transform: translateX(-100%);
    transition: transform .22s ease;
  }

  .rail.open {
    transform: translateX(0);
  }
}
</style>
