import request from '@/utils/request'
import type { Agent, IRawPlanResponse } from '@/stores'

export interface ActionHistory {
  ID: string
  ActionType: string
  AgentName: string
  Description: string
  ImportantInput: string[]
  Action_Result: string
}

export type IExecuteRawResponse = {
  LogNodeType: string
  NodeId: string
  InputName_List?: string[] | null
  OutputName?: string
  content?: string
  ActionHistory: ActionHistory[]
}

class Api {
  // 智能体信息
  setAgents = (data: Pick<Agent, 'Name' | 'Profile' | 'apiUrl' | 'apiKey' | 'apiModel'>[]) => {
    return request({
      url: '/setAgents',
      data,
      method: 'POST',
    })
  }

  generateBasePlan = (data: {
    goal: string
    inputs: string[]
    apiUrl?: string
    apiKey?: string
    apiModel?: string
  }) => {
    return request<unknown, IRawPlanResponse>({
      url: '/generate_basePlan',
      method: 'POST',
      data: {
        'General Goal': data.goal,
        'Initial Input Object': data.inputs,
        apiUrl: data.apiUrl,
        apiKey: data.apiKey,
        apiModel: data.apiModel,
      },
    })
  }

  executePlan = (plan: IRawPlanResponse) => {
    return request<unknown, IExecuteRawResponse[]>({
      url: '/executePlan',
      method: 'POST',
      data: {
        RehearsalLog: [],
        num_StepToRun: null,
        plan: {
          'Initial Input Object': plan['Initial Input Object'],
          'General Goal': plan['General Goal'],
          'Collaboration Process': plan['Collaboration Process']?.map((step) => ({
            StepName: step.StepName,
            TaskContent: step.TaskContent,
            InputObject_List: step.InputObject_List,
            OutputObject: step.OutputObject,
            AgentSelection: step.AgentSelection,
            Collaboration_Brief_frontEnd: step.Collaboration_Brief_FrontEnd,
            TaskProcess: step.TaskProcess.map((action) => ({
              ActionType: action.ActionType,
              AgentName: action.AgentName,
              Description: action.Description,
              ID: action.ID,
              ImportantInput: action.ImportantInput,
            })),
          })),
        },
      },
    })
  }
}

export default new Api()
