import client from './client'

export interface FlowNodeConfig {
  [key: string]: any
}

export interface FlowNode {
  id: string
  type: string
  config: FlowNodeConfig
}

export interface FlowEdge {
  source: string
  target: string
  label?: string
}

export interface FlowDefinition {
  nodes: FlowNode[]
  edges: FlowEdge[]
}

export interface FlowRecord {
  id: number
  flowCode: string
  flowName: string
  flowType: number
  status: number
  definitionJson: string | FlowDefinition
  version?: number
  createTime?: string
  updateTime?: string
}

export interface SaveFlowPayload {
  flowCode: string
  flowName: string
  flowType?: string
  definitionJson: FlowDefinition
}

export function listFlows() {
  return client.get('/api/v1/inbound/flow/list')
}

export function getFlow(flowCode: string) {
  return client.get(`/api/v1/inbound/flow/${flowCode}`)
}

export function saveFlow(data: SaveFlowPayload) {
  return client.post('/api/v1/inbound/flow', {
    ...data,
    flowType: data.flowType || 'inbound',
  })
}

export function updateFlow(flowCode: string, data: SaveFlowPayload) {
  return client.post(`/api/v1/inbound/flow/${flowCode}`, {
    ...data,
    flowType: data.flowType || 'inbound',
  })
}

export function publishFlow(flowCode: string) {
  return client.post(`/api/v1/inbound/flow/${flowCode}/publish`)
}

export function offlineFlow(flowCode: string) {
  return client.post(`/api/v1/inbound/flow/${flowCode}/offline`)
}

export function deleteFlow(flowCode: string) {
  return client.delete(`/api/v1/inbound/flow/${flowCode}`)
}
