<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../../stores/auth'
import { useAgentStore } from '../../stores/agents'
import { useTaskStore } from '../../stores/tasks'
import { navigation, icons } from '../../composables/icons'
import type { NavGroupDef } from '../../composables/icons'

defineProps<{
  mobileOpen?: boolean
}>()

defineEmits<{
  close: []
}>()
import NavGroup from './NavGroup.vue'
import { initials } from '../../utils/format'

const auth = useAuthStore()
const router = useRouter()
const agentStore = useAgentStore()
const taskStore = useTaskStore()

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

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

/**
 * Dynamische Nav-Item-Counts aus den Stores.
 * Überschreibt die hartcodierten `count`-Werte im navigation-Array.
 */
const dynamicNavigation = computed<NavGroupDef[]>(() => {
  // Deep-clone: Jede Gruppe und jedes Item neu erstellen
  return navigation.map(group => ({
    ...group,
    items: group.items.map(item => {
      let dynamicCount: string | undefined

      switch (item.label) {
        case 'Agenten':
        case 'Hosts · OpenClaw':
          dynamicCount = String(agentStore.agentList.length)
          break
        case 'Task Board':
          dynamicCount = String(taskStore.taskList.length)
          break
        case 'Kosten & Tokens':
          dynamicCount = agentStore.todayCost
          break
        case 'Docs & .md':
          dynamicCount = '0'
          break
        case 'Incidents':
          dynamicCount = '0'
          break
      }

      return {
        ...item,
        count: dynamicCount ?? item.count,
      }
    }),
  }))
})
</script>

<template>
  <aside :class="['sidebar', { open: mobileOpen }]">
    <button class="sidebar-close" @click="$emit('close')" v-html="icons.chevron_left || ''"></button>
    <!-- Brand -->
    <div class="side-top">
      <div class="brand-mark" v-html="icons.command || ''"></div>
      <div>
        <div class="brand-name">NEXUS</div>
        <div class="brand-sub">Mission Control</div>
      </div>
    </div>

    <!-- Navigation -->
    <nav class="nav">
      <NavGroup
        v-for="(group, idx) in dynamicNavigation"
        :key="idx"
        :label="group.group"
        :items="group.items"
      />
    </nav>

    <!-- Footer -->
    <div class="side-foot">
      <div class="avatar">{{ ownerInitials }}</div>
      <div class="owner-info">
        <div class="owner-name">{{ auth.user?.displayName ?? 'Owner' }}</div>
        <div class="owner-role">{{ auth.user?.role ?? 'Owner' }}</div>
      </div>
    </div>
  </aside>
</template>

<style scoped>
.sidebar {
  width: 248px;
  flex: 0 0 248px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, rgba(14,12,32,.92), rgba(8,6,20,.92));
  border-right: 1px solid var(--line);
  backdrop-filter: blur(14px);
  padding: 0;
  position: relative;
  z-index: 2;
}

.side-top {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 18px 18px 16px;
}

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

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

.brand-name {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 17px;
  letter-spacing: .14em;
  line-height: 1;
}

.brand-sub {
  font-size: 10.5px;
  color: var(--tx-3);
  letter-spacing: .05em;
  margin-top: 3px;
}

.nav {
  flex: 1;
  overflow-y: auto;
  padding: 6px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.side-foot {
  padding: 12px;
  border-top: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  transition: background .15s;
}

.side-foot:hover {
  background: rgba(124,108,255,.06);
}

.sidebar-close {
  display: none;
}

@media (max-width: 767px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 100;
    height: 100vh;
    width: 280px;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-close {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 18px;
    right: 12px;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--tx-2);
    cursor: pointer;
    z-index: 1;
  }

  .sidebar-close:hover {
    background: rgba(124,108,255,.1);
    color: var(--tx);
  }

  .sidebar-close :deep(svg) {
    width: 18px;
    height: 18px;
  }
}

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

.owner-info {
  min-width: 0;
}

.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);
  margin-top: 1px;
  text-transform: capitalize;
}
</style>
