import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Message, Conversation } from '@/api/conversation'

export const useConversationStore = defineStore('conversation', () => {
  const conversations = ref<Conversation[]>([])
  const currentConversationId = ref<string | null>(null)
  const messages = ref<Message[]>([])
  const isLoading = ref(false)

  function setConversations(data: Conversation[]) {
    conversations.value = data
  }

  function setCurrentConversation(id: string | null) {
    currentConversationId.value = id
  }

  function addMessage(msg: Message) {
    messages.value.push(msg)
  }

  function removeMessage(id: string) {
    messages.value = messages.value.filter((msg) => msg.id !== id)
  }

  function setMessages(data: Message[]) {
    messages.value = data
  }

  function addConversation(conv: Conversation) {
    conversations.value.unshift(conv)
  }

  function removeConversation(id: string) {
    conversations.value = conversations.value.filter((c) => c.id !== id)
  }

  return {
    conversations,
    currentConversationId,
    messages,
    isLoading,
    setConversations,
    setCurrentConversation,
    addMessage,
    removeMessage,
    setMessages,
    addConversation,
    removeConversation,
  }
})
