import axios from 'axios'

const http = axios.create({
  baseURL: process.env.VUE_APP_PYTHON_API,
  timeout: 300000, // 5分钟超时，用于大数据量分析
})

export async function uploadFile(file) {
  const form = new FormData()
  form.append('file', file)
  // 不要手动设置 Content-Type，让浏览器自动添加 boundary
  const { data } = await http.post('/api/sku-lifecycle/upload', form, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
  return data
}

export async function analyzeFile(file) {
  // Upload file first
  const uploadResult = await uploadFile(file)
  if (!uploadResult.success) {
    throw new Error(uploadResult.message || 'File upload failed')
  }

  // Analyze cached data
  const { data } = await http.post('/api/sku-lifecycle/analyze')
  return data
}



// export async function analyzeProductLifecycle(file) {
//   const form = new FormData()
//   form.append('file', file)
//   // 不要手动设置 Content-Type，让浏览器自动添加 boundary
//   const { data } = await http.post('/sku-lifecycle/analyze-product', form)
//   return data
// }

export async function getResults() {
  const { data } = await http.get('/api/sku-lifecycle/results')
  return data
}

export async function getResultBySku(sku) {
  const { data } = await http.get(`/api/sku-lifecycle/results/${encodeURIComponent(sku)}`)
  return data
}

// SPU 生命周期相关 API
export async function uploadSpuFile(file) {
  const form = new FormData()
  form.append('file', file)
  const { data } = await http.post('/api/spu-lifecycle/upload', form, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
  return data
}

export async function analyzeSpuFile(file) {
  const uploadResult = await uploadSpuFile(file)
  if (!uploadResult.success) {
    throw new Error(uploadResult.message || 'File upload failed')
  }

  const { data } = await http.post('/api/spu-lifecycle/analyze')
  return data
}

export async function getSpuResults() {
  const { data } = await http.get('/api/spu-lifecycle/results')
  return data
}

export async function getResultBySpu(spu) {
  const { data } = await http.get(`/api/spu-lifecycle/results/${encodeURIComponent(spu)}`)
  return data
}

export async function getCacheInfo() {
  const { data } = await http.get('/api/sku-lifecycle/cache/info')
  return data
}

export async function clearBackendCache() {
  const { data } = await http.delete('/api/sku-lifecycle/cache')
}

export async function simulateStrategy(payload) {
  const { data } = await http.post('/api/sku-lifecycle/strategy-simulation', payload)
  return data
}

export async function getInsights(payload) {
  const { data } = await http.post('/api/sku-lifecycle/insights', payload)
  return data
}

// SKU 生命周期策略模拟相关 API（显式命名，便于页面直接对接）
export async function simulateSkuLifecycleStrategy(payload) {
  return simulateStrategy(payload)
}

export async function getSkuLifecycleInsights(payload) {
  return getInsights(payload)
}

// 爆款分析相关API
export async function uploadHotProductFile(file) {
  const form = new FormData()
  form.append('file', file)
  const { data } = await http.post('/api/sku-lifecycle/upload', form, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
  return data
}

export async function analyzeHotProduct() {
  const { data } = await http.post('/api/hotproduct/analyze')
  return data
}

export async function analyzeHotProductWithFile(file) {
  // Upload file first
  const uploadResult = await uploadHotProductFile(file)
  if (!uploadResult.success) {
    throw new Error(uploadResult.message || 'File upload failed')
  }

  // Analyze cached data
  return await analyzeHotProduct()
}

// SPU 爆款分析相关 API
export async function analyzeSpuHotProductWithFile(file) {
  // Upload file first
  const uploadResult = await uploadSpuFile(file)
  if (!uploadResult.success) {
    throw new Error(uploadResult.message || 'File upload failed')
  }

  const { data } = await http.post('/api/spu-hotproduct/analyze')
  return data
}

export async function getSpuHotProductResults() {
  const { data } = await http.get('/api/spu-hotproduct/results')
  return data
}

export async function getSpuHotProductResultBySpu(spu) {
  const { data } = await http.get(`/api/spu-hotproduct/results/${encodeURIComponent(spu)}`)
  return data
}



export async function getHotProductResults() {
  const { data } = await http.get('/api/hotproduct/results')
  return data
}

export async function getHotProductResultBySku(sku) {
  const { data } = await http.get(`/api/hotproduct/results/${encodeURIComponent(sku)}`)
  return data
}

// 销售概览相关API
export async function uploadSaleOverviewFile(file) {
  const form = new FormData()
  form.append('file', file)
  const { data } = await http.post('/api/sale-overview/upload', form, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
  return data
}

export async function analyzeSaleOverview() {
  const { data } = await http.post('/api/sale-overview/analyze')
  return data
}

export async function analyzeSaleOverviewWithFile(file) {
  // Upload file first
  const uploadResult = await uploadSaleOverviewFile(file)
  if (!uploadResult.success) {
    throw new Error(uploadResult.message || 'File upload failed')
  }

  // Analyze cached data
  return await analyzeSaleOverview()
}

export async function getSaleOverviewResults() {
  const { data } = await http.get('/api/sale-overview/results')
  return data
}

// 销售趋势预测相关API
export async function uploadSaleTrendFile(file) {
  const form = new FormData()
  form.append('file', file)
  const { data } = await http.post('/api/sale-trend/upload', form, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
  return data
}

export async function analyzeSaleTrend() {
  const { data } = await http.post('/api/sale-trend/analyze')
  return data
}

export async function analyzeSaleTrendWithFile(file) {
  // Upload file first
  const uploadResult = await uploadSaleTrendFile(file)
  if (!uploadResult.success) {
    throw new Error(uploadResult.message || 'File upload failed')
  }

  // Analyze cached data
  return await analyzeSaleTrend()
}

export async function getSaleTrendResults() {
  const { data } = await http.get('/api/sale-trend/results')
  return data
}

export async function predictSalesTrend(params) {
  const { data } = await http.post('/api/sale-trend/predict', params)
  return data
}

// 促销效果分析相关API
export async function uploadSaleEffectFile(file) {
  const form = new FormData()
  form.append('file', file)
  const { data } = await http.post('/api/sale-effect/upload', form, {
    headers: { 'Content-Type': 'multipart/form-data' }
  })
  return data
}

export async function analyzeSaleEffect() {
  const { data } = await http.post('/api/sale-effect/analyze')
  return data
}

export async function analyzeSaleEffectWithFile(file) {
  // Upload file first
  const uploadResult = await uploadSaleEffectFile(file)
  if (!uploadResult.success) {
    throw new Error(uploadResult.message || 'File upload failed')
  }

  // Analyze cached data
  return await analyzeSaleEffect()
}

export async function getSaleEffectResults() {
  const { data } = await http.get('/api/sale-effect/results')
  return data
}
