import api from './index.js'

// 点播上传相关 API

// 创建视频上传地址与凭证（根据接口文档）
export const createUploadAuth = (data) => {
  console.log('[VOD] createUploadAuth 请求入参:', data)
  // data: { title, description, tags, cateId, coverUrl, fileName, fileSize }
  return api.post('/vod/upload-auth', data).then(res => {
    console.log('[VOD] createUploadAuth 响应:', res)
    // 后端返回 { code, message, data: { uploadAuth, uploadAddress, videoId, requestId } }
    return res?.data || res
  }).catch(err => {
    console.error('[VOD] createUploadAuth 错误:', err)
    throw err
  })
}

// 刷新视频上传凭证（断点续传或凭证过期）
export const refreshUploadAuth = (data) => {
  console.log('[VOD] refreshUploadAuth 请求入参:', data)
  // data: { videoId }
  return api.post('/vod/refresh-upload-auth', data).then(res => {
    console.log('[VOD] refreshUploadAuth 响应:', res)
    // 后端返回 { code, message, data: { uploadAuth, uploadAddress, videoId, requestId } }
    return res?.data || res
  }).catch(err => {
    console.error('[VOD] refreshUploadAuth 错误:', err)
    throw err
  })
}

// 获取播放凭证
export const getPlayAuth = (data) => {
  console.log('[VOD] getPlayAuth 请求入参:', data)
  // data: { videoId, authInfoTimeout?, playDomain? }
  return api.post('/vod/play-auth', data).then(res => {
    console.log('[VOD] getPlayAuth 响应:', res)
    return res?.data || res
  }).catch(err => {
    console.error('[VOD] getPlayAuth 错误:', err)
    throw err
  })
}

// 获取播放凭证并更新播放次数
export const getPlayAuthWithViewCount = (data) => {
  console.log('[VOD] getPlayAuthWithViewCount 请求入参:', data)
  // data: { videoId, courseId, chapterId, authInfoTimeout?, playDomain? }
  return api.post('/vod/play-auth-with-view-count', data).then(res => {
    console.log('[VOD] getPlayAuthWithViewCount 响应:', res)
    return res?.data || res
  }).catch(err => {
    console.error('[VOD] getPlayAuthWithViewCount 错误:', err)
    throw err
  })
}

// 获取视频信息
export const getVideoInfo = (data) => {
  console.log('[VOD] getVideoInfo 请求入参:', data)
  // data: { videoId }
  return api.post('/vod/video-info', data).then(res => {
    console.log('[VOD] getVideoInfo 响应:', res)
    return res?.data || res
  }).catch(err => {
    console.error('[VOD] getVideoInfo 错误:', err)
    throw err
  })
}


