import { apiClient } from './client'
import type { AIActionType } from '@/types'

export interface SendMessageParams {
  sessionId: string
  content: string
  actionType?: AIActionType
  focalNodeId?: string | null
  flowContext?: Record<string, unknown> | null
}

export const editorAIApi = {
  startSession: async (flowType: 'troubleshooting' | 'procedural', treeId?: string) => {
    const { data } = await apiClient.post('/ai/chat/sessions', {
      flow_type: flowType,
      tree_id: treeId,
    })
    return data
  },

  sendMessage: async ({ sessionId, content, actionType, focalNodeId, flowContext }: SendMessageParams) => {
    const { data } = await apiClient.post(`/ai/chat/sessions/${sessionId}/messages`, {
      content,
      action_type: actionType || 'open_chat',
      focal_node_id: focalNodeId,
      flow_context: flowContext || undefined,
    })
    return data
  },

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

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

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