import { request } from './request'

export interface ChatMsg {
  role: 'user' | 'ai'
  content: string
  type?: string
  createdAt: string
}

export async function sendMessage(message: string, conversationId: string, treeId?: number): Promise<string> {
  const res = await request<{ reply: string }>('/mobile/chat', {
    method: 'POST',
    body: JSON.stringify({ message, treeId, conversationId }),
  })
  return res.data.reply
}

export async function fetchHistory(treeId: number, page = 1, pageSize = 10): Promise<{ records: ChatMsg[]; total: number }> {
  const res = await request<{ records: ChatMsg[]; total: number }>(
    `/mobile/chat/history?treeId=${treeId}&page=${page}&pageSize=${pageSize}`,
  )
  return { records: res.data.records || [], total: res.data.total || 0 }
}
