<script setup lang="ts">
import type { ChatMessage } from '@/types/chat'
import { useScroll } from '@vueuse/core'
import { isDark, toggleDark } from '@/composables/dark'


// Sample chat data for demonstration
const messages = ref<ChatMessage[]>([
])

const inputText = ref('')
const isLoading = ref(false)
const messagesContainer = ref<HTMLElement>()

const { arrivedState } = useScroll(messagesContainer, {
  offset: { bottom: 100 },
})

// Auto-scroll to bottom when new messages arrive
watch(messages, () => {
  nextTick(() => {
    if (messagesContainer.value && arrivedState.bottom) {
      messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
    }
  })
}, { deep: true })

function handleSend(text: string) {
  if (!text.trim())
    return

  // Add user message
  const userMessage: ChatMessage = {
    id: Date.now(),
    content: text,
    sender: 'user',
    timestamp: new Date(),
    status: 'sent',
  }
  messages.value.push(userMessage)

  // Clear input
  inputText.value = ''

  // Simulate AI response after delay
  isLoading.value = true
  setTimeout(() => {
    userMessage.status = 'sent'

    const aiMessage: ChatMessage = {
      id: Date.now() + 1,
      content: `I received your message: "${text}". This is a simulated response.`,
      sender: 'ai',
      timestamp: new Date(),
      status: 'sent',
    }
    messages.value.push(aiMessage)
    isLoading.value = false
  }, 1000)
}

function handleKeydown(e: KeyboardEvent) {
  // You can add additional keyboard shortcuts here
  console.log('Keydown event:', e.key)
}
</script>

<template>
  <div class="bg-gray-50 flex flex-col h-screen dark:bg-gray-950">

    <!-- Messages container -->
    <van-list
      ref="messagesContainer"
      class="p-4 pb-24 flex-1"
      :finished="true"
      :loading="false"
      finished-text=""
      loading-text=""
    >
      <van-empty
        v-if="messages.length === 0"
        description="No messages yet. Start a conversation!"
        class="mt-20"
      />

      <div v-else>
        <ChatBubble
          v-for="message in messages"
          :key="message.id"
          :message="message"
          :show-timestamp="true"
        />
      </div>
    </van-list>

    <!-- Input area -->
    <div class="bottom-0 left-0 right-0 fixed z-10">
      <ChatInput
        v-model="inputText"
        :disabled="isLoading"
        :loading="isLoading"
        placeholder="Type your message..."
        @send="handleSend"
        @keydown="handleKeydown"
      />
    </div>
  </div>
</template>

<route lang="json5">
{
  name: 'LLMChat',
  meta: {
    title: 'AI Chat',
    requiresAuth: false
  }
}
</route>
