import api from './api'
import type { Conversation, Message, QueryResult } from '../types'

export const chatApi = {
  listConversations: (limit = 50) =>
    api.get<Conversation[]>('/conversations', { params: { limit } }),

  createConversation: (title?: string) =>
    api.post<Conversation>('/conversations', title ? { title } : undefined),

  getConversation: (id: string) =>
    api.get<Conversation & { messages: Message[] }>(`/conversations/${id}`),

  updateConversation: (id: string, title: string) =>
    api.put<Conversation>(`/conversations/${id}`, { title }),

  deleteConversation: (id: string) =>
    api.delete(`/conversations/${id}`),

  getMessages: (conversationId: string) =>
    api.get<Message[]>(`/conversations/${conversationId}/messages`),

  chat: (question: string, conversationId?: string) =>
    api.post('/chat', { question, conversation_id: conversationId }),

  getResults: (messageId: string) =>
    api.get<QueryResult>(`/results/${messageId}`),

  getResultCsvUrl: (messageId: string) =>
    `/api/results/${messageId}/csv`,
}
