import axios from 'axios'

const request = axios.create({
  baseURL: '/api/v1',
  timeout: 60000
})

// Request interceptor
request.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// Response interceptor
request.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    const message = error.response?.data?.message || error.message || '请求失败'
    console.error('API Error:', message)
    return Promise.reject(error)
  }
)

export const projectApi = {
  list: () => request.get('/projects/'),
  get: (id) => request.get(`/projects/${id}`),
  create: (data) => request.post('/projects/', data),
  update: (id, data) => request.put(`/projects/${id}`, data),
  delete: (id) => request.delete(`/projects/${id}`)
}

export const fileApi = {
  upload: (projectId, formData) =>
    request.post(`/projects/${projectId}/files/upload`, formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    }),
  list: (projectId) => request.get(`/projects/${projectId}/files/`),
  get: (projectId, fileId) => request.get(`/projects/${projectId}/files/${fileId}`),
  delete: (projectId, fileId) => request.delete(`/projects/${projectId}/files/${fileId}`)
}

export const chunkApi = {
  split: (projectId, data) => request.post(`/projects/${projectId}/chunks/split`, data),
  list: (projectId, params) => request.get(`/projects/${projectId}/chunks/`, { params }),
  get: (projectId, chunkId) => request.get(`/projects/${projectId}/chunks/${chunkId}`),
  update: (projectId, chunkId, data) => request.put(`/projects/${projectId}/chunks/${chunkId}`, data),
  delete: (projectId, chunkId) => request.delete(`/projects/${projectId}/chunks/${chunkId}`)
}

export const questionApi = {
  generate: (projectId, data) => request.post(`/projects/${projectId}/generate-questions`, data),
  list: (projectId, params) => request.get(`/projects/${projectId}/chunks/${params.chunkId}/questions`),
  update: (projectId, questionId, data) => request.put(`/projects/${projectId}/questions/${questionId}`, data),
  delete: (projectId, questionId) => request.delete(`/projects/${projectId}/questions/${questionId}`)
}

export const datasetApi = {
  list: (projectId) => request.get(`/projects/${projectId}/datasets/`),
  create: (projectId, data) => request.post(`/projects/${projectId}/datasets/`, data),
  get: (projectId, datasetId) => request.get(`/projects/${projectId}/datasets/${datasetId}`),
  delete: (projectId, datasetId) => request.delete(`/projects/${projectId}/datasets/${datasetId}`),
  export: (projectId, datasetId, data) =>
    request.post(`/projects/${projectId}/datasets/${datasetId}/export`, data, {
      responseType: 'blob'
    })
}

export const evalApi = {
  list: (projectId) => request.get(`/projects/${projectId}/eval-datasets/`),
  create: (projectId, data) => request.post(`/projects/${projectId}/eval-datasets/`, data),
  run: (projectId, evalId) => request.post(`/projects/${projectId}/eval-datasets/${evalId}/evaluate`),
  getResults: (projectId, taskId) => request.get(`/projects/${projectId}/eval-tasks/${taskId}`)
}

export default request
