<template>
  <div class="chat-page">
    <!-- 左侧：会话列表 -->
    <ChatSessionSidebar
      :sessions="sessions"
      :current-session-id="currentSessionId"
      :has-active-run-fn="hasActiveRunForSession"
      @select="selectSession"
      @new="newSession"
      @delete="onDeleteSession"
      @rename="onRenameSession"
      @toggle-pin="onTogglePinSession"
    />

    <!-- 中间：消息列表 + 输入框 -->
    <div class="chat-main">
      <div v-if="!currentSessionId" class="chat-empty">
        <n-empty description="选择左侧会话或点击「新建会话」开始对话" />
      </div>
      <template v-else>
        <ChatMessageList
          :messages="messages"
          :pending-ask="pendingAsk"
          @answer="onAnswer"
          @regenerate="onRegenerate"
          @edit="onEdit"
          @switch-version="onSwitchVersion"
        />
        <ChatComposer
          :sending="sending"
          :has-session="!!currentSessionId"
          :default-model-id="session?.modelId"
          :default-skill-folder="session?.skillFolder"
          @send="onSend"
          @export="exportCurrentSession"
          @reset="resetCurrentSession"
          @delete="deleteCurrentSession"
        />
      </template>
    </div>
  </div>
</template>

<script setup>
import { onMounted, onBeforeUnmount } from 'vue'
import { NEmpty } from 'naive-ui'
import ChatSessionSidebar from '../../components/chat/ChatSessionSidebar.vue'
import ChatMessageList from '../../components/chat/ChatMessageList.vue'
import ChatComposer from '../../components/chat/ChatComposer.vue'
import { useChat } from '../../composables/useChat'
import { useGlobalRuns } from '../../composables/useGlobalRuns'

const {
  sessions,
  currentSessionId,
  session,
  messages,
  sending,
  pendingAsk,
  loadSessions,
  selectSession,
  newSession,
  send,
  answerClarify,
  deleteCurrentSession,
  deleteSessionById,
  renameSession,
  togglePinSession,
  exportCurrentSession,
  resetCurrentSession,
  regenerateMessage,
  editUserMessage,
  activateVersion
} = useChat()

const { hasActiveRunForSession, cleanupFinished } = useGlobalRuns()

onMounted(() => {
  loadSessions()
})

// 组件卸载时清理所有非 RUNNING 的 run（已完成/失败的 run）。RUNNING 中的 run 保留。
onBeforeUnmount(() => {
  cleanupFinished()
})

function onSend({ text, opts }) {
  send(text, opts)
}

function onAnswer({ clarifyId, answer }) {
  answerClarify(clarifyId, answer)
}

function onRegenerate(messageId) {
  regenerateMessage(messageId)
}

function onEdit({ messageId, content }) {
  editUserMessage(messageId, content)
}

function onSwitchVersion(messageId) {
  activateVersion(messageId)
}

function onDeleteSession(sessionId) {
  // 已经在 Sidebar 内做了 useDialog 二次确认，这里直接执行
  deleteSessionById(sessionId)
}

function onRenameSession(sessionId, title) {
  renameSession(sessionId, title)
}

function onTogglePinSession(sessionId, currentPinned) {
  togglePinSession(sessionId, !!currentPinned)
}
</script>

<style scoped>
.chat-page {
  display: flex;
  height: 100%;
  min-height: 0;
}
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
