import { apiClient } from './client'
import type {
  AIChatStartResponse,
  AIChatMessageResponse,
  AIChatSessionResponse,
  AIChatGenerateResponse,
  AIChatImportResponse,
} from '@/types'

export const aiChatApi = {
  startSession: async (flowType: 'troubleshooting' | 'procedural'): Promise<AIChatStartResponse> => {
    const { data } = await apiClient.post('/ai/chat/sessions', { flow_type: flowType })
    return data
  },

  sendMessage: async (sessionId: string, content: string): Promise<AIChatMessageResponse> => {
    const { data } = await apiClient.post(`/ai/chat/sessions/${sessionId}/messages`, { content })
    return data
  },

  getSession: async (sessionId: string): Promise<AIChatSessionResponse> => {
    const { data } = await apiClient.get(`/ai/chat/sessions/${sessionId}`)
    return data
  },

  generateTree: async (sessionId: string): Promise<AIChatGenerateResponse> => {
    const { data } = await apiClient.post(`/ai/chat/sessions/${sessionId}/generate`)
    return data
  },

  importTree: async (
    sessionId: string,
    params?: { name?: string; description?: string; category_id?: string; tags?: string[] }
  ): Promise<AIChatImportResponse> => {
    const { data } = await apiClient.post(`/ai/chat/sessions/${sessionId}/import`, params || {})
    return data
  },

  abandonSession: async (sessionId: string): Promise<void> => {
    await apiClient.delete(`/ai/chat/sessions/${sessionId}`)
  },
}

export default aiChatApi
