<template>
  <aside class="df-sidebar">
    <!-- Logo -->
    <div class="sidebar-brand">
      <div class="brand-mark">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
          <path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z" fill="url(#brand-grad)" />
          <defs><linearGradient id="brand-grad" x1="3" y1="2" x2="21" y2="22">
            <stop stop-color="#7b6ff0"/><stop offset="1" stop-color="#5eaff0"/>
          </linearGradient></defs>
        </svg>
      </div>
      <span class="brand-text">DevFlow</span>
      <span class="brand-version">v0.1</span>
    </div>

    <!-- Navigation -->
    <nav class="sidebar-nav">
      <div class="nav-group">
        <span class="nav-group-label">{{ $t('nav.workspace') }}</span>
        <router-link
          v-for="item in primaryNav" :key="item.path"
          :to="item.path"
          class="nav-link"
          active-class="nav-link--active"
        >
          <span class="nav-link-icon" v-html="item.svg"></span>
          <span class="nav-link-text">{{ $t(item.label) }}</span>
          <span v-if="item.badge" class="nav-link-badge">{{ item.badge }}</span>
        </router-link>
      </div>

      <div class="nav-divider"></div>

      <div class="nav-group">
        <span class="nav-group-label">{{ $t('nav.traceability') }}</span>
        <router-link
          v-for="item in secondaryNav" :key="item.path"
          :to="item.path"
          class="nav-link"
          active-class="nav-link--active"
        >
          <span class="nav-link-icon" v-html="item.svg"></span>
          <span class="nav-link-text">{{ $t(item.label) }}</span>
          <span v-if="item.path === '/knowledge' && candidateCount > 0" class="nav-link-badge">{{ candidateCount }}</span>
        </router-link>
      </div>
    </nav>

    <!-- Footer -->
    <div class="sidebar-footer">
      <div class="sidebar-status">
        <span class="status-dot"></span>
        <span class="status-text">{{ $t('nav.systemReady') }}</span>
      </div>
      <!-- AI 面板切换按钮 -->
      <button class="nav-link ai-toggle-btn" :class="{ 'ai-toggle-btn--active': aiStore.state.panelOpen }" @click="aiStore.togglePanel()">
        <span class="nav-link-icon">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 2a7 7 0 014 12.74V17a1 1 0 01-1 1H9a1 1 0 01-1-1v-2.26A7 7 0 0112 2z"/><line x1="9" y1="21" x2="15" y2="21"/></svg>
        </span>
        <span class="nav-link-text">{{ $t('nav.aiPanel') }}</span>
        <span class="ai-toggle-shortcut">⌘I</span>
      </button>
      <router-link to="/settings" class="nav-link" active-class="nav-link--active">
        <span class="nav-link-icon">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-4 0v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83-2.83l.06-.06A1.65 1.65 0 004.68 15a1.65 1.65 0 00-1.51-1H3a2 2 0 010-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 012.83-2.83l.06.06A1.65 1.65 0 009 4.68a1.65 1.65 0 001-1.51V3a2 2 0 014 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 2.83l-.06.06A1.65 1.65 0 0019.4 9a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></svg>
        </span>
        <span class="nav-link-text">{{ $t('nav.settings') }}</span>
      </router-link>
    </div>
  </aside>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useAiStore } from '@/stores/ai'
import { useKnowledgeStore } from '@/stores/knowledge'

const aiStore = useAiStore()
const knowledgeStore = useKnowledgeStore()

// 知识库候选数量(侧栏 badge)— 解包 computed 取值保持响应式
const candidateCount = computed(() => knowledgeStore.candidateCount)

const primaryNav = [
  { path: '/dashboard', label: 'nav.overview', badge: null,
    svg: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/></svg>' },
  { path: '/ideas', label: 'nav.ideas', badge: null,
    svg: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M9 18h6M10 22h4M12 2a7 7 0 014 12.74V17a1 1 0 01-1 1h-6a1 1 0 01-1-1v-2.26A7 7 0 0112 2z"/></svg>' },
  { path: '/projects', label: 'nav.projects', badge: null,
    svg: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M22 19a2 2 0 01-2 2H4a2 2 0 01-2-2V5a2 2 0 012-2h5l2 3h9a2 2 0 012 2z"/></svg>' },
  { path: '/tasks', label: 'nav.tasks', badge: null,
    svg: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"/></svg>' },
]

const secondaryNav = [
  { path: '/knowledge', label: 'nav.knowledge',
    svg: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 19.5A2.5 2.5 0 016.5 17H20"/><path d="M6.5 2H20v20H6.5A2.5 2.5 0 014 19.5v-15A2.5 2.5 0 016.5 2z"/></svg>' },
  { path: '/audit', label: 'nav.audit',
    svg: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 01-2 2H5a2 2 0 01-2-2V5a2 2 0 012-2h11"/></svg>' },
]
</script>

<style scoped>
.df-sidebar {
  width: 192px;
  min-width: 192px;
  background: var(--df-sidebar-bg);
  border-right: 0.5px solid var(--df-border);
  display: flex;
  flex-direction: column;
  position: relative;
  user-select: none;
}

/* — Logo — */
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 16px 12px;
  border-bottom: 0.5px solid var(--df-border);
}
.brand-mark {
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  background: var(--df-accent-bg);
  border: 0.5px solid var(--df-border);
}
.brand-text {
  font-size: 15px;
  font-weight: 500;
  letter-spacing: -0.3px;
  color: var(--df-text);
}
.brand-version {
  font-family: var(--df-font-mono);
  font-size: 10px;
  color: var(--df-text-dim);
  background: rgba(255,255,255,0.04);
  padding: 1px 6px;
  border-radius: var(--df-radius-xs);
  margin-left: auto;
}

/* — Nav — */
.sidebar-nav { flex: 1; overflow-y: auto; padding: 8px 8px; }

.nav-group { margin-bottom: 2px; }
.nav-group-label {
  display: block;
  padding: 8px 10px 4px;
  font-size: 9px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--df-text-dim);
}

.nav-divider {
  height: 0.5px;
  background: var(--df-border);
  margin: 6px 12px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  margin: 1px 0;
  border-radius: var(--df-radius-sm);
  color: var(--df-text-secondary);
  text-decoration: none;
  font-size: 12px;
  font-weight: 450;
  transition: all 0.18s var(--df-ease);
  position: relative;
}

.nav-link:hover {
  background: var(--df-sidebar-hover);
  color: var(--df-text);
}
.nav-link--active {
  background: var(--df-sidebar-active);
  color: var(--df-accent);
  font-weight: 500;
}
.nav-link--active::before {
  content: '';
  position: absolute;
  left: -10px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 16px;
  background: var(--df-accent);
  border-radius: 0 3px 3px 0;
}

.nav-link-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  opacity: 0.7;
  flex-shrink: 0;
}
.nav-link--active .nav-link-icon { opacity: 1; }

.nav-link-text { flex: 1; }

.nav-link-badge {
  font-family: var(--df-font-mono);
  font-size: 10px;
  font-weight: 500;
  padding: 1px 6px;
  border-radius: var(--df-radius);
  min-width: 18px;
  text-align: center;
  background: var(--df-accent);
  color: #fff;
  line-height: 1.4;
}

/* — AI Toggle Button — */
.ai-toggle-btn {
  cursor: pointer;
  border: none;
  background: transparent;
  width: 100%;
  font: inherit;
  text-align: left;
  appearance: none;
  -webkit-appearance: none;
}
.ai-toggle-btn--active {
  color: var(--df-accent);
  background: var(--df-sidebar-active);
}
.ai-toggle-shortcut {
  font-family: var(--df-font-mono);
  font-size: 9px;
  color: var(--df-text-dim);
  background: rgba(255,255,255,0.04);
  padding: 1px 5px;
  border-radius: var(--df-radius-sm);
}

/* — Footer — */
.sidebar-footer {
  padding: 6px 8px;
  border-top: 0.5px solid var(--df-border);
}

.sidebar-status {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  margin-bottom: 1px;
}
.status-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--df-success);
  animation: pulseGlow 3s ease-in-out infinite;
}
.status-text {
  font-family: var(--df-font-mono);
  font-size: 10px;
  color: var(--df-text-dim);
  letter-spacing: 0.3px;
}

/* ═══ Responsive ═══ */
@media (max-width: 768px) {
  .df-sidebar { width: 56px; min-width: 56px; }
  .brand-text, .brand-version, .nav-group-label, .nav-link-text,
  .nav-link-badge, .sidebar-status, .status-text, .ai-toggle-shortcut { display: none; }
  .sidebar-brand { padding: 12px 10px 10px; justify-content: center; }
  .brand-mark { width: 32px; height: 32px; }
  .nav-link { justify-content: center; padding: 8px 0; }
  .nav-divider { margin: 4px 8px; }
  .nav-link--active::before { display: none; }
}
</style>
