import axios from 'axios'

const http = axios.create({
  baseURL: process.env.VUE_APP_PYTHON_API,
  timeout: 300000
})

function uploadLifecycleFile(file, url) {
  const form = new FormData()
  form.append('file', file)
  return http.post(url, form, {
    headers: { 'Content-Type': 'multipart/form-data' }
  }).then(({ data }) => data)
}

export async function uploadFile(file) {
  return uploadLifecycleFile(file, '/api/sku-lifecycle/upload')
}

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

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

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

export async function uploadSpuFile(file) {
  return uploadLifecycleFile(file, '/api/spu-lifecycle/upload')
}

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 uploadHotProductFile(file) {
  return uploadLifecycleFile(file, '/api/sku-lifecycle/upload')
}

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

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

  return analyzeHotProduct()
}

async function uploadSpuHotProductFile(file) {
  return uploadLifecycleFile(file, '/api/spu-lifecycle/upload')
}

export async function analyzeSpuHotProductWithFile(file) {
  const uploadResult = await uploadSpuHotProductFile(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 getHotProductResults() {
  const { data } = await http.get('/api/hotproduct/results')
  return data
}

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

// export async function analyzeProductLifecycle(file) {
//   return analyzeFile(file)
// }

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

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

export async function simulateSkuLifecycleStrategy(payload) {
  return simulateStrategy(payload)
}

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