import { apiClient } from './client'
import type {
  AIQuotaStatus,
  AIStartResponse,
  AIScaffoldResponse,
  AIBranchDetailResponse,
  AIAssembleResponse,
} from '@/types'

export const aiBuilderApi = {
  getQuota: async (): Promise<AIQuotaStatus> => {
    const { data } = await apiClient.get('/ai/quota')
    return data
  },

  start: async (params: {
    flow_type: 'troubleshooting' | 'procedural'
    name: string
    description: string
    environment_tags?: string[]
    category_id?: string
  }): Promise<AIStartResponse> => {
    const { data } = await apiClient.post('/ai/start', params)
    return data
  },

  scaffold: async (conversationId: string): Promise<AIScaffoldResponse> => {
    const { data } = await apiClient.post('/ai/scaffold', {
      conversation_id: conversationId,
    })
    return data
  },

  branchDetail: async (
    conversationId: string,
    branchName: string
  ): Promise<AIBranchDetailResponse> => {
    const { data } = await apiClient.post('/ai/branch-detail', {
      conversation_id: conversationId,
      branch_name: branchName,
    })
    return data
  },

  assemble: async (
    conversationId: string,
    selectedBranches: Array<{
      name: string
      description: string
      steps?: Record<string, unknown>
    }>
  ): Promise<AIAssembleResponse> => {
    const { data } = await apiClient.post('/ai/assemble', {
      conversation_id: conversationId,
      selected_branches: selectedBranches,
    })
    return data
  },
}

export default aiBuilderApi
