import api from './client'

export interface ChatMessage {
  id: string
  role: 'user' | 'assistant' | 'system'
  content: string
  created_at: string
}

export interface ChatSession {
  session_id: string
  last_message: string
  message_count: number
  created_at: string
}

export interface ChatResponse {
  session_id: string
  user_message: ChatMessage
  assistant_message: ChatMessage
}

export async function sendChatMessage(
  message: string,
  sessionId?: string,
  contextType?: string,
  contextId?: string,
): Promise<ChatResponse> {
  const res = await api.post<ChatResponse>('/chat/messages', {
    message,
    session_id: sessionId || undefined,
    context_type: contextType || undefined,
    context_id: contextId || undefined,
  })
  return res.data
}

export async function getChatSessions(): Promise<ChatSession[]> {
  const res = await api.get<ChatSession[]>('/chat/sessions')
  return res.data
}

export async function getSessionMessages(sessionId: string): Promise<ChatMessage[]> {
  const res = await api.get<ChatMessage[]>(`/chat/sessions/${sessionId}/messages`)
  return res.data
}

export async function deleteChatSession(sessionId: string): Promise<void> {
  await api.delete(`/chat/sessions/${sessionId}`)
}
