<template>
  <aside
      class="chat-sidebar"
      :class="{ collapsed: isCollapsed }"
      :style="{ width: isCollapsed ? '0px' : `${sidebarWidth}px` }"
  >
    <div class="sidebar-inner">
      <!-- 头部 -->
      <div class="sidebar-header">
        <div class="logo">
          <Bot :size="24" class="logo-icon" />
          <span v-show="!isCollapsed" class="logo-text">AI Chat</span>
        </div>
        <button
            class="collapse-btn"
            @click="toggleSidebar"
            :title="isCollapsed ? '展开侧边栏' : '收起侧边栏'"
        >
          <ChevronLeft :size="18" :class="{ rotated: isCollapsed }" />
        </button>
      </div>

      <!-- 新建对话按钮 -->
      <div class="new-chat-section">
        <button class="new-chat-btn" @click="handleNewChat">
          <Plus :size="18" />
          <span>新建对话</span>
        </button>
      </div>

      <!-- 搜索框 -->
      <div class="search-section">
        <div class="search-box" @click="openSearch">
          <Search :size="16" />
          <span class="search-placeholder">搜索对话...</span>
          <kbd class="search-kbd">⌘K</kbd>
        </div>
      </div>

      <!-- 对话列表 -->
      <div class="conversations-section">
        <!-- 置顶对话 -->
        <div v-if="pinnedConversations.length > 0" class="conversation-group">
          <div class="group-header">
            <Pin :size="14" />
            <span>置顶</span>
          </div>
          <div class="group-list">
            <ConversationItem
                v-for="conv in pinnedConversations"
                :key="conv.id"
                :conversation="conv"
                :is-active="conv.id === currentConversationId"
                @select="selectConversation"
                @delete="deleteConversation"
                @rename="renameConversation"
                @toggle-pin="togglePinConversation"
            />
          </div>
        </div>

        <!-- 最近对话 -->
        <div class="conversation-group">
          <div class="group-header">
            <Clock :size="14" />
            <span>最近</span>
          </div>
          <div class="group-list">
            <ConversationItem
                v-for="conv in recentConversations"
                :key="conv.id"
                :conversation="conv"
                :is-active="conv.id === currentConversationId"
                @select="selectConversation"
                @delete="deleteConversation"
                @rename="renameConversation"
                @toggle-pin="togglePinConversation"
            />
          </div>
        </div>

        <!-- 空状态 -->
        <div
            v-if="pinnedConversations.length === 0 && recentConversations.length === 0"
            class="empty-state"
        >
          <MessageSquare :size="40" class="empty-icon" />
          <p>暂无对话</p>
          <span>点击上方按钮开始新对话</span>
        </div>
      </div>

      <!-- 底部操作 -->
      <div class="sidebar-footer">
        <button class="footer-btn" @click="toggleTheme" title="切换主题">
          <Sun v-if="currentTheme === 'light'" :size="18" />
          <Moon v-else-if="currentTheme === 'dark'" :size="18" />
          <Monitor v-else :size="18" />
        </button>
        <button class="footer-btn" @click="openShortcuts" title="快捷键">
          <Keyboard :size="18" />
        </button>
        <button class="footer-btn" @click="openSettings" title="设置">
          <Settings :size="18" />
        </button>
      </div>
    </div>

    <!-- 拖拽调整宽度 -->
    <div
        class="resize-handle"
        @mousedown="startResize"
    />
  </aside>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useChatStore } from '@/stores/chat'
import { useSettingsStore } from '@/stores/settings'
import ConversationItem from './ConversationItem.vue'
import {
  Bot,
  Plus,
  Search,
  Pin,
  Clock,
  MessageSquare,
  Sun,
  Moon,
  Monitor,
  Keyboard,
  Settings,
  ChevronLeft,
} from '@/components/icons'

const chatStore = useChatStore()
const settingsStore = useSettingsStore()

const {
  currentConversationId,
  pinnedConversations,
  recentConversations
} = storeToRefs(chatStore)

const {
  sidebarCollapsed: isCollapsed,
  sidebarWidth,
  settings
} = storeToRefs(settingsStore)

const currentTheme = computed(() => settings.value.theme)

// 方法
function handleNewChat() {
  chatStore.createConversation()
}

function selectConversation(id: string) {
  chatStore.selectConversation(id)
}

function deleteConversation(id: string) {
  chatStore.deleteConversation(id)
}

function renameConversation(id: string, title: string) {
  chatStore.renameConversation(id, title)
}

function togglePinConversation(id: string) {
  chatStore.togglePinConversation(id)
}

function toggleSidebar() {
  settingsStore.toggleSidebar()
}

function toggleTheme() {
  settingsStore.toggleTheme()
}

function openShortcuts() {
  settingsStore.openShortcutsModal()
}

function openSettings() {
  settingsStore.openSettingsModal()
}

function openSearch() {
  settingsStore.openSearchModal()
}

// 拖拽调整宽度
const isResizing = ref(false)

function startResize(e: MouseEvent) {
  isResizing.value = true
  const startX = e.clientX
  const startWidth = sidebarWidth.value

  const handleMouseMove = (e: MouseEvent) => {
    const diff = e.clientX - startX
    settingsStore.setSidebarWidth(startWidth + diff)
  }

  const handleMouseUp = () => {
    isResizing.value = false
    document.removeEventListener('mousemove', handleMouseMove)
    document.removeEventListener('mouseup', handleMouseUp)
  }

  document.addEventListener('mousemove', handleMouseMove)
  document.addEventListener('mouseup', handleMouseUp)
}
</script>

<style lang="scss" scoped>
.chat-sidebar {
  position: relative;
  height: 100vh;
  background: #f8fafc;
  border-right: 1px solid #e2e8f0;
  transition: width 0.3s ease;
  overflow: hidden;
  flex-shrink: 0;

  .dark & {
    background: #1e1e2e;
    border-right-color: #2d2d3d;
  }

  &.collapsed {
    .sidebar-inner {
      opacity: 0;
      pointer-events: none;
    }
  }
}

.sidebar-inner {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  transition: opacity 0.2s ease;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid #e2e8f0;

  .dark & {
    border-bottom-color: #2d2d3d;
  }
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  color: #3b82f6;
}

.logo-text {
  font-size: 18px;
  font-weight: 700;
  color: #1f2937;

  .dark & {
    color: #f3f4f6;
  }
}

.collapse-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: #6b7280;
  cursor: pointer;
  transition: all 0.2s ease;

  &:hover {
    background: rgba(0, 0, 0, 0.05);
    color: #374151;

    .dark & {
      background: rgba(255, 255, 255, 0.05);
      color: #e5e7eb;
    }
  }

  svg {
    transition: transform 0.3s ease;

    &.rotated {
      transform: rotate(180deg);
    }
  }
}

.new-chat-section {
  padding: 12px 16px;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 12px 16px;
  border: 1px dashed #d1d5db;
  border-radius: 12px;
  background: transparent;
  color: #374151;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;

  .dark & {
    border-color: #4b5563;
    color: #e5e7eb;
  }

  &:hover {
    border-color: #3b82f6;
    background: rgba(59, 130, 246, 0.05);
    color: #3b82f6;
  }
}

.search-section {
  padding: 0 16px 12px;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.03);
  color: #9ca3af;
  cursor: pointer;
  transition: all 0.2s ease;

  .dark & {
    background: rgba(255, 255, 255, 0.03);
  }

  &:hover {
    background: rgba(0, 0, 0, 0.06);

    .dark & {
      background: rgba(255, 255, 255, 0.06);
    }
  }
}

.search-placeholder {
  flex: 1;
  font-size: 13px;
}

.search-kbd {
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.06);

  .dark & {
    background: rgba(255, 255, 255, 0.1);
  }
}

.conversations-section {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 12px;
}

.conversation-group {
  margin-bottom: 8px;
}

.group-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 20px;
  font-size: 12px;
  font-weight: 600;
  color: #9ca3af;
  text-transform: uppercase;
  letter-spacing: 0.5px;

  .dark & {
    color: #6b7280;
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;

  .empty-icon {
    color: #d1d5db;
    margin-bottom: 12px;

    .dark & {
      color: #4b5563;
    }
  }

  p {
    margin: 0 0 4px;
    font-size: 14px;
    font-weight: 500;
    color: #6b7280;
  }

  span {
    font-size: 12px;
    color: #9ca3af;
  }
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid #e2e8f0;

  .dark & {
    border-top-color: #2d2d3d;
  }
}

.footer-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: #6b7280;
  cursor: pointer;
  transition: all 0.2s ease;

  &:hover {
    background: rgba(0, 0, 0, 0.05);
    color: #374151;

    .dark & {
      background: rgba(255, 255, 255, 0.05);
      color: #e5e7eb;
    }
  }
}

.resize-handle {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;

  &:hover {
    background: rgba(59, 130, 246, 0.3);
  }
}
</style>