import api from './index.js'

/**
 * 工作流相关API接口
 */

// 获取工作流分类列表
export const getWorkflowCategories = () => {
  return api.get('/workflow/categories')
}

// 获取热门工作流列表
export const getHotWorkflows = (params = {}) => {
  return api.get('/workflow/hot', { params })
}

// 根据分类获取工作流列表
export const getWorkflowsByCategory = (category, params = {}) => {
  return api.get(`/workflow/category/${category}`, { params })
}

// 获取工作流详情
export const getWorkflowDetail = (id) => {
  return api.get(`/workflow/${id}/detail`)
}

// 获取工作流列表
export const getWorkflowList = (params = {}) => {
  return api.get('/workflow/list', { params })
}

// 复制工作流
export const copyWorkflow = (id) => {
  return api.post(`/workflow/copy/${id}`)
}

// 保存/更新工作流
export const saveWorkflow = (data) => {
  return api.post('/workflow/save', data)
}

// 更新工作流信息（根据接口文档）
export const updateWorkflow = (id, data) => {
  console.log('[Workflow] updateWorkflow 请求入参:', { id, data })
  // data: { name?, description?, coverUrl?, detailGallery?, category?, isPublic?, fullAccessRole?, copyAccessRole?, price?, isFree?, data?, dataFileUrl?, vodVideoId?, videoId? }
  return api.put(`/content/workflows/${id}`, data).then(res => {
    console.log('[Workflow] updateWorkflow 响应:', res)
    return res
  }).catch(err => {
    console.error('[Workflow] updateWorkflow 错误:', err)
    throw err
  })
}

// 删除工作流
export const deleteWorkflow = (id) => {
  return api.delete(`/workflow/${id}`)
}

// 点赞工作流
export const likeWorkflow = (id) => {
  return api.post(`/workflow/${id}/like`)
}

// 取消点赞工作流
export const unlikeWorkflow = (id) => {
  return api.delete(`/workflow/${id}/like`)
}

// 获取我的工作流列表
export const getMyWorkflows = (params = {}) => {
  return api.get('/workflow/my', { params })
}

// 创建/保存工作流作品（根据接口文档）
export const submitWorkflow = (data) => {
  console.log('[Workflow] submitWorkflow 请求入参:', data)
  // data: { name?, description?, data?, dataFileUrl?, coverUrl?, videoId?, detailGallery?, category?, isFree?, isPublic?, fullAccessRole?, copyAccessRole? }
  return api.post('/workflow/submit', data).then(res => {
    console.log('[Workflow] submitWorkflow 响应:', res)
    // 后端返回 { code, message, data: workflowId }
    // 返回完整的响应对象，包含 code, message, data
    return res
  }).catch(err => {
    console.error('[Workflow] submitWorkflow 错误:', err)
    throw err
  })
}

// 收藏/取消收藏工作流
export const toggleWorkflowFavorite = (workflowId) => {
  return api.post('/v1/workflow-likes/toggle', {
    workflowId: workflowId
  })
}

// 获取工作流数据包下载地址
export const getWorkflowDataFile = (workflowId) => {
  return api.get(`/workflow/${workflowId}/data-file`)
}

// 获取工作流JSON数据
export const getWorkflowJsonData = (workflowId) => {
  return api.get(`/workflow/${workflowId}/json-data`)
}

// 检查用户是否已点赞指定工作流
export const checkWorkflowLikeStatus = (workflowId) => {
  return api.get('/v1/workflow-likes/check', {
    params: { workflowId }
  })
} 