<template>
  <div class="chat-view main-panel">
    <div v-if="!conversationId" class="welcome">
      <div class="welcome-icon">
        <svg viewBox="0 0 64 64" width="36" height="36">
          <rect width="64" height="64" rx="14" fill="url(#favBg)"/>
          <defs>
            <linearGradient id="favBg" x1="0%" y1="0%" x2="100%" y2="100%">
              <stop offset="0%" stop-color="#2563eb"/>
              <stop offset="100%" stop-color="#60a5fa"/>
            </linearGradient>
          </defs>
          <text x="32" y="40" text-anchor="middle" font-family="-apple-system,BlinkMacSystemFont,sans-serif" font-size="18" font-weight="800" fill="#fff" letter-spacing="-0.5">Luxx</text>
        </svg>
      </div>
      <h1>Chat</h1>
      <p>选择一个对话开始，或创建新对话</p>
    </div>

    <template v-else>
      <div class="chat-header">
        <div class="chat-title-area">
          <h2 class="chat-title">{{ conversationTitle || '新对话' }}</h2>
        </div>
      </div>

      <div ref="messagesContainer" class="messages-container" @scroll="handleScroll">
        <div v-if="loading" class="load-more-top">
          <span>加载中...</span>
        </div>

        <div class="messages-list">
          <div
            v-for="msg in messages"
            :key="msg.id"
            :data-msg-id="msg.id"
          >
            <MessageBubble
              :role="msg.role"
              :text="msg.text || msg.content"
              :tool-calls="msg.tool_calls"
              :process-steps="msg.process_steps"
              :token-count="msg.token_count"
              :created-at="msg.created_at"
              :deletable="msg.role === 'user'"
              :attachments="msg.attachments"
              @delete="deleteMessage(msg.id)"
            />
          </div>

          <!-- 流式消息 -->
          <div v-if="streamingMessage" class="message-bubble assistant streaming">
            <div class="avatar">Luxx</div>
            <div class="message-body">
              <ProcessBlock
                :process-steps="streamingMessage.process_steps"
                :streaming="true"
              />
            </div>
          </div>
        </div>
      </div>

      <div class="message-input">
        <div class="input-container">
          <textarea
            ref="textareaRef"
            v-model="inputMessage"
            :placeholder="sending ? 'AI 正在回复中...' : '输入消息... (Shift+Enter 换行)'"
            rows="1"
            @input="autoResize"
            @keydown="onKeydown"
          ></textarea>
          <div class="input-footer">
            <div class="input-actions">
              <button
                class="btn-send"
                :class="{ active: canSend }"
                :disabled="!canSend || sending"
                @click="sendMessage"
              >
                <span v-html="sendIcon"></span>
              </button>
            </div>
          </div>
        </div>
        <div class="input-hint">AI 助手回复内容仅供参考</div>
      </div>
    </template>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, nextTick, watch } from 'vue'
import { useRoute } from 'vue-router'
import { conversationsAPI, messagesAPI } from '../services/api.js'
import ProcessBlock from '../components/ProcessBlock.vue'
import MessageBubble from '../components/MessageBubble.vue'
import { renderMarkdown } from '../utils/markdown.js'

const route = useRoute()
const messages = ref([])
const inputMessage = ref('')
const loading = ref(true)
const sending = ref(false)
const streamingMessage = ref(null)
const messagesContainer = ref(null)
const textareaRef = ref(null)
const autoScroll = ref(true)
const conversationId = ref(route.params.id)
const conversationTitle = ref('')

const canSend = computed(() => inputMessage.value.trim().length > 0)

const sendIcon = `<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>`

function autoResize() {
  const el = textareaRef.value
  if (!el) return
  el.style.height = 'auto'
  el.style.height = Math.min(el.scrollHeight, 200) + 'px'
}

function onKeydown(e) {
  if (e.key === 'Enter' && !e.shiftKey) {
    e.preventDefault()
    sendMessage()
  }
}

const loadMessages = async () => {
  autoScroll.value = true
  loading.value = true
  try {
    const res = await messagesAPI.list(conversationId.value)
    if (res.success) {
      messages.value = res.data.messages || []
      if (messages.value.length > 0) {
        conversationTitle.value = res.data.title || ''
      }
    }
  } catch (e) {
    console.error(e)
  } finally {
    loading.value = false
    scrollToBottom()
  }
}

const deleteMessage = async (msgId) => {
  try {
    await messagesAPI.delete(msgId)
    messages.value = messages.value.filter(m => m.id !== msgId)
  } catch (e) {
    console.error(e)
  }
}

const sendMessage = async () => {
  if (!inputMessage.value.trim() || sending.value) return
  
  const content = inputMessage.value.trim()
  inputMessage.value = ''
  sending.value = true
  
  // 清空输入框
  nextTick(() => {
    autoResize()
  })
  
  // 添加用户消息
  messages.value.push({
    id: Date.now(),
    role: 'user',
    content: content,
    text: content,
    attachments: [],
    process_steps: [],
    created_at: new Date().toISOString()
  })
  scrollToBottom()
  
  // 初始化流式消息
  streamingMessage.value = {
    id: Date.now() + 1,
    role: 'assistant',
    process_steps: [],
    created_at: new Date().toISOString()
  }
  
  // SSE 流式请求
  messagesAPI.sendStream(
    { conversation_id: conversationId.value, content },
    {
      onProcessStep: (step) => {
        autoScroll.value = true  // 流式开始时启用自动滚动
        if (!streamingMessage.value) return
        // 按 id 更新或追加步骤
        const idx = streamingMessage.value.process_steps.findIndex(s => s.id === step.id)
        if (idx >= 0) {
          streamingMessage.value.process_steps[idx] = step
        } else {
          streamingMessage.value.process_steps.push(step)
        }
      },
      onDone: () => {
        // 完成，添加到消息列表
        autoScroll.value = true
        if (streamingMessage.value) {
          messages.value.push({
            ...streamingMessage.value,
            created_at: new Date().toISOString()
          })
          streamingMessage.value = null
        }
        sending.value = false
      },
      onError: (error) => {
        console.error('Stream error:', error)
        if (streamingMessage.value) {
          streamingMessage.value.process_steps.push({
            id: 'error-' + Date.now(),
            index: streamingMessage.value.process_steps.length,
            type: 'text',
            content: `[错误] ${error}`
          })
        }
        sending.value = false
      }
    }
  )
  
  scrollToBottom()
}

const scrollToBottom = () => {
  if (!autoScroll.value) return
  nextTick(() => {
    if (messagesContainer.value) {
      messagesContainer.value.scrollTo({
        top: messagesContainer.value.scrollHeight,
        behavior: streamingMessage.value ? 'instant' : 'smooth'
      })
    }
  })
}

// 处理滚动事件，检测用户是否手动滚动
const handleScroll = () => {
  if (!messagesContainer.value) return
  const { scrollTop, scrollHeight, clientHeight } = messagesContainer.value
  const distanceToBottom = scrollHeight - scrollTop - clientHeight
  // 距离底部超过50px时停止自动跟随
  autoScroll.value = distanceToBottom < 50
}

// 监听流式消息变化，自动滚动
watch(() => streamingMessage.value?.process_steps?.length, () => {
  if (streamingMessage.value) {
    scrollToBottom()
  }
})

const formatTime = (time) => {
  if (!time) return ''
  return new Date(time).toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
}

onMounted(loadMessages)
</script>

<style scoped>
.chat-view {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  min-width: 0;
}

.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-tertiary);
}

.welcome-icon {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  overflow: hidden;
}

.welcome h1 {
  font-size: 24px;
  color: var(--text-primary);
  margin: 0 0 8px;
}

.welcome p {
  font-size: 14px;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border-light);
  background: color-mix(in srgb, var(--bg-primary) 70%, transparent);
  backdrop-filter: blur(40px);
  -webkit-backdrop-filter: blur(40px);
  transition: background 0.2s, border-color 0.2s;
}

.chat-title-area {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chat-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.messages-container {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.messages-container::-webkit-scrollbar {
  display: none;
}

.load-more-top {
  text-align: center;
  padding: 12px 0;
  color: var(--text-tertiary);
  font-size: 13px;
}

.messages-list {
  width: 80%;
  margin: 0 auto;
  padding: 0 16px;
}

/* Message Input */
.message-input {
  padding: 16px 24px 12px;
  background: var(--bg-primary);
  border-top: 1px solid var(--border-light);
  transition: background 0.2s, border-color 0.2s;
}

.input-container {
  display: flex;
  flex-direction: column;
  background: var(--bg-input);
  border: 1px solid var(--border-input);
  border-radius: 12px;
  padding: 12px;
  transition: border-color 0.2s, background 0.2s;
}

.input-container:focus-within {
  border-color: var(--accent-primary);
}

textarea {
  width: 100%;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.6;
  resize: none;
  outline: none;
  font-family: inherit;
  min-height: 36px;
  max-height: 200px;
  padding: 0;
}

textarea::placeholder {
  color: var(--text-tertiary);
}

.input-footer {
  display: flex;
  justify-content: flex-end;
  padding-top: 8px;
  margin-top: 4px;
}

.input-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-send {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: var(--bg-code);
  color: var(--text-tertiary);
  cursor: not-allowed;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

.btn-send.active {
  background: var(--accent-primary);
  color: white;
  cursor: pointer;
}

.btn-send.active:hover {
  background: var(--accent-primary-hover);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
}

.btn-send.active:active {
  transform: translateY(0);
  box-shadow: none;
}

.input-hint {
  text-align: center;
  font-size: 12px;
  color: var(--text-tertiary);
  margin-top: 8px;
}
</style>
