import axios from 'axios'

export const api = axios.create({
  baseURL: '/api',
  headers: {
    'Content-Type': 'application/json',
  },
})

export const uploadFile = async (file: File): Promise<{ task_id: string; filename: string; size: number }> => {
  const formData = new FormData()
  formData.append('file', file)

  const response = await api.post('/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })

  return response.data
}

export const startSplit = async (
  task_id: string,
  tracklist: TracklistEntry[],
  options: SplitOptions
): Promise<{ task_id: string; status: string }> => {
  const response = await api.post('/split', {
    task_id,
    tracklist,
    options,
  })
  return response.data
}

export const getStatus = async (task_id: string): Promise<TaskStatus> => {
  const response = await api.get(`/status/${task_id}`)
  return response.data
}

export const getDownloadUrl = (task_id: string): string => {
  return `/api/download/${task_id}`
}

export const getDownloadZipUrl = (task_id: string): string => {
  return `/api/download/${task_id}/splits.zip`
}