import apiClient from './client'
import type {
  AssistantChat,
  ChatListItem,
  ChatMessageResponse,
  RetentionSettings,
} from '@/types/assistant-chat'

export const assistantChatApi = {
  async createChat(): Promise<AssistantChat> {
    const response = await apiClient.post<AssistantChat>('/assistant/chats', {})
    return response.data
  },

  async listChats(page = 1, size = 20): Promise<ChatListItem[]> {
    const response = await apiClient.get<ChatListItem[]>('/assistant/chats', {
      params: { page, size },
    })
    return response.data
  },

  async getChat(chatId: string): Promise<AssistantChat> {
    const response = await apiClient.get<AssistantChat>(`/assistant/chats/${chatId}`)
    return response.data
  },

  async sendMessage(chatId: string, message: string): Promise<ChatMessageResponse> {
    const response = await apiClient.post<ChatMessageResponse>(
      `/assistant/chats/${chatId}/messages`,
      { message }
    )
    return response.data
  },

  async updateChat(chatId: string, data: { title?: string; pinned?: boolean }): Promise<AssistantChat> {
    const response = await apiClient.patch<AssistantChat>(`/assistant/chats/${chatId}`, data)
    return response.data
  },

  async deleteChat(chatId: string): Promise<void> {
    await apiClient.delete(`/assistant/chats/${chatId}`)
  },

  async bulkDeleteChats(olderThanDays: number): Promise<void> {
    await apiClient.delete('/assistant/chats', { params: { older_than_days: olderThanDays } })
  },

  async getRetentionSettings(): Promise<RetentionSettings> {
    const response = await apiClient.get<RetentionSettings>('/assistant/retention')
    return response.data
  },

  async updateRetentionSettings(data: Partial<RetentionSettings>): Promise<RetentionSettings> {
    const response = await apiClient.patch<RetentionSettings>('/assistant/retention', data)
    return response.data
  },
}

export default assistantChatApi
