import { http } from '@/http/http'
import type { IProcessDefinition, IProcessInstance, ITask } from './types/workflow'

/**
 * 流程定义管理
 */
export const workflowAPI = {
  // 获取流程定义列表
  getProcessDefinitions() {
    return http.get<IProcessDefinition[]>('/v1/workflow/definitions')
  },

  // 获取流程定义详情
  getProcessDefinition(id: number) {
    return http.get<IProcessDefinition>(`/v1/workflow/definitions/${id}`)
  },

  // 创建流程定义
  createProcessDefinition(data: Partial<IProcessDefinition>) {
    return http.post<IProcessDefinition>('/v1/workflow/definitions', data)
  },

  // 更新流程定义
  updateProcessDefinition(id: number, data: Partial<IProcessDefinition>) {
    return http.put<IProcessDefinition>(`/v1/workflow/definitions/${id}`, data)
  },

  // 删除流程定义
  deleteProcessDefinition(id: number) {
    return http.delete(`/v1/workflow/definitions/${id}`)
  },

  // 启动流程实例
  startInstance(data: {
    process_code: string
    business_type: string
    business_id: number
    variables?: Record<string, any>
  }) {
    return http.post<IProcessInstance>('/v1/workflow/instances', data)
  },

  // 获取流程实例列表
  getProcessInstances(params?: {
    business_type?: string
    business_id?: number
    status?: string
  }) {
    return http.get<IProcessInstance[]>('/v1/workflow/instances', params)
  },

  // 获取流程实例详情
  getInstanceDetail(id: number) {
    return http.get<{
      instance: IProcessInstance
      tasks: ITask[]
    }>(`/v1/workflow/instances/${id}`)
  },

  // 获取待办任务
  getPendingTasks() {
    return http.get<ITask[]>('/v1/workflow/tasks/pending')
  },

  // 审批通过任务
  approveTask(id: number, comment?: string) {
    return http.post(`/v1/workflow/tasks/${id}/approve`, { comment })
  },

  // 审批拒绝任务
  rejectTask(id: number, comment?: string) {
    return http.post(`/v1/workflow/tasks/${id}/reject`, { comment })
  }
}

export default workflowAPI
