import api from './request'

// 获取我的作品列表
export const getMyWorks = (params = {}) => {
  return api.get('/works/my-works', {
    params: {
      page: params.page || 0,
      size: params.size || 10,
      includeProcessing: params.includeProcessing !== false, // 默认包含正在处理中的作品
      workType: params.workType || null // 按作品类型筛选
    }
  })
}

// 按类型获取我的作品（用于历史记录）
export const getMyWorksByType = (workType, params = {}) => {
  return api.get('/works/my-works', {
    params: {
      page: params.page || 0,
      size: params.size || 20,
      includeProcessing: true,
      workType: workType // TEXT_TO_VIDEO, IMAGE_TO_VIDEO, STORYBOARD_VIDEO, STORYBOARD_IMAGE
    }
  })
}

// 获取正在进行中的作品
export const getProcessingWorks = () => {
  return api.get('/works/processing')
}

// 获取作品详情
export const getWorkDetail = (workId) => {
  return api.get(`/works/${workId}`)
}

// 删除作品
export const deleteWork = (workId) => {
  return api.delete(`/works/${workId}`)
}

// 批量删除作品
export const batchDeleteWorks = (workIds) => {
  return api.post('/works/batch-delete', {
    workIds: workIds
  })
}

// 更新作品信息
export const updateWork = (workId, data) => {
  return api.put(`/works/${workId}`, data)
}

// 获取作品统计信息
export const getWorkStats = () => {
  return api.get('/works/stats')
}

// 记录下载（增加下载次数）
export const recordDownload = (workId) => {
  return api.post(`/works/${workId}/download`)
}

// 获取作品文件下载URL
export const getWorkFileUrl = (workId, download = false) => {
  // 构建URL，直接返回完整路径用于浏览器打开
  const baseUrl = import.meta.env.VITE_API_BASE_URL || '/api'
  
  // 去掉 /api 前缀，因为 nginx 会自动转发
  let url = `${baseUrl}/works/${workId}/file`
  
  // 添加 download 参数（可选）
  if (download) {
    url += '?download=true'
  }
  
  return url
}









