import axios from 'axios';

const api = axios.create({
  baseURL: '/api',
  timeout: 10000
});

api.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

export default {
  // 活动相关
  getActivities: (params) => api.get('/activities', { params }),
  getActivity: (id) => api.get(`/activities/${id}`),
  
  // 报名相关
  submitRegistration: (data) => api.post('/registrations', data),
  getRegistration: (id) => api.get(`/registrations/${id}`),
  getActivityRegistrations: (id) => api.get(`/activities/${id}/registrations`),
  
  // 用户上传照片
  uploadUserPhotos: (id, data) => api.post(`/activities/${id}/photos`, data, {
    headers: { 'Content-Type': 'multipart/form-data' },
    timeout: 300000 // 5分钟超时
  }),
  
  // 用户验证登录
  verifyUser: (activityId, data) => api.post(`/auth/verify/${activityId}`, data),
  
  // 管理员相关
  adminLogin: (data) => api.post('/admin/login', data),
  createActivity: (data) => api.post('/admin/activities', data),
  updateActivity: (id, data) => api.put(`/admin/activities/${id}`, data),
  approveRegistration: (id, status) => api.put(`/admin/registrations/${id}`, { status }),
  updateRegistration: (id, data) => api.put(`/admin/registrations/${id}`, data),
  checkInRegistration: (id) => api.put(`/admin/registrations/${id}/checkin`),
  getAllRegistrations: (params) => api.get('/admin/registrations', { params }),
  addExpense: (data) => api.post('/admin/expenses', data, {
    headers: { 'Content-Type': 'multipart/form-data' },
    timeout: 60000
  }),
  getExpenses: (params) => api.get('/admin/expenses', { params }),
  updateExpense: (id, data) => api.put(`/admin/expenses/${id}`, data, {
    headers: { 'Content-Type': 'multipart/form-data' },
    timeout: 60000
  }),
  deleteExpense: (id) => api.delete(`/admin/expenses/${id}`),
  uploadPhotos: (data) => api.post('/admin/photos', data, {
    headers: { 'Content-Type': 'multipart/form-data' },
    timeout: 300000 // 5分钟超时，适配大文件上传
  }),
  getPendingPhotos: () => api.get('/admin/photos/pending'),
  approvePhoto: (id, status) => api.put(`/admin/photos/${id}`, { status }),
  deletePhoto: (id) => api.delete(`/admin/photos/${id}`),
  addSponsor: (data) => api.post('/admin/sponsors', data, {
    headers: { 'Content-Type': 'multipart/form-data' },
    timeout: 60000 // 1分钟超时
  }),
  updateSponsor: (id, data) => api.put(`/admin/sponsors/${id}`, data, {
    headers: { 'Content-Type': 'multipart/form-data' },
    timeout: 60000
  }),
  deleteSponsor: (id) => api.delete(`/admin/sponsors/${id}`)
};
