import api from '../utils/request'

export const paymentChannelApi = {
  list: () => api.get('/payment-channels'),
}

export const uploadApi = {
  uploadImage: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return api.post('/upload', formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },
  uploadMultiple: (files: File[]) => {
    const formData = new FormData()
    files.forEach(file => formData.append('files', file))
    return api.post('/upload/multiple', formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },
  deleteImage: (filename: string) => api.delete(`/upload/${filename}`)
}

export const systemApi = {
  checkInstalled: () => api.get('/installed'),
  install: (data: any) => api.post('/install', data),
  getPublicSettings: () => api.get('/settings/public'),
}

export const authApi = {
  register: (data: any) => api.post('/auth/register', data),
  login: (data: any) => api.post('/auth/login', data),
  forgotPassword: (data: any) => api.post('/auth/forgot-password', data),
  resetPassword: (data: any) => api.post('/auth/reset-password', data),
  verifyEmail: (data: any) => api.post('/auth/verify-email', data),
  sendVerifyCode: () => api.post('/users/send-verify-code'),
}

export const userApi = {
  getProfile: () => api.get('/users/profile'),
  updateProfile: (data: any) => api.put('/users/profile', data),
  changePassword: (data: any) => api.put('/users/password', data),
}

export const addressApi = {
  list: () => api.get('/users/addresses'),
  create: (data: any) => api.post('/users/addresses', data),
  update: (id: number, data: any) => api.put(`/users/addresses/${id}`, data),
  delete: (id: number) => api.delete(`/users/addresses/${id}`),
  setDefault: (id: number) => api.put(`/users/addresses/${id}/default`),
}

export const categoryApi = {
  list: () => api.get('/categories'),
}

export const brandApi = {
  list: () => api.get('/brands'),
}

export const productApi = {
  list: (params?: any) => api.get('/products', { params }),
  getById: (id: number) => api.get(`/products/${id}`),
}

export const cartApi = {
  list: () => api.get('/cart'),
  add: (data: any) => api.post('/cart', data),
  update: (id: number, data: any) => api.put(`/cart/${id}`, data),
  delete: (id: number) => api.delete(`/cart/${id}`),
}

export const orderApi = {
  list: () => api.get('/orders'),
  getById: (id: number) => api.get(`/orders/${id}`),
  create: (data: any) => api.post('/orders', data),
  refund: (id: number, data: any) => api.post(`/orders/${id}/refund`, data),
  cancel: (id: number) => api.put(`/orders/${id}/cancel`),
  confirmReceipt: (id: number) => api.put(`/orders/${id}/confirm-receipt`),
  createPayment: (id: number) => api.post(`/orders/${id}/pay`),
  checkPaymentStatus: (id: number) => api.get(`/orders/${id}/payment-status`),
}

export const articleApi = {
  list: (params?: any) => api.get('/articles', { params }),
  getById: (id: number) => api.get(`/articles/${id}`),
}

export const bannerApi = {
  list: () => api.get('/banners'),
}

export const lotteryApi = {
  list: () => api.get('/lotteries'),
  getById: (id: number) => api.get(`/lotteries/${id}`),
  register: (id: number) => api.post(`/lotteries/${id}/register`),
}

export const ticketApi = {
  list: (params?: any) => api.get('/tickets', { params }),
  create: (data: any) => api.post('/tickets', data),
  getById: (id: number) => api.get(`/tickets/${id}`),
  reply: (id: number, data: any) => api.post(`/tickets/${id}/reply`, data),
}

export const supplierApi = {
  getOrders: () => api.get('/supplier/orders'),
  getOrderById: (id: number) => api.get(`/supplier/orders/${id}`),
  confirmOrder: (id: number) => api.put(`/supplier/orders/${id}/confirm`),
  shipOrder: (id: number, data: any) => api.put(`/supplier/orders/${id}/ship`, data),
  getInventory: () => api.get('/supplier/inventory'),
  updateInventory: (id: number, data: any) => api.put(`/supplier/inventory/${id}`, data),
}

export const adminApi = {
  getStats: () => api.get('/admin/stats'),
  getUsers: () => api.get('/admin/users'),
  updateUser: (id: number, data: any) => api.put(`/admin/users/${id}`, data),
  deleteUser: (id: number) => api.delete(`/admin/users/${id}`),

  getCategories: () => api.get('/categories'),
  createCategory: (data: any) => api.post('/admin/categories', data),
  updateCategory: (id: number, data: any) => api.put(`/admin/categories/${id}`, data),
  deleteCategory: (id: number) => api.delete(`/admin/categories/${id}`),

  getBrands: () => api.get('/admin/brands'),
  createBrand: (data: any) => api.post('/admin/brands', data),
  updateBrand: (id: number, data: any) => api.put(`/admin/brands/${id}`, data),
  deleteBrand: (id: number) => api.delete(`/admin/brands/${id}`),

  getProducts: () => api.get('/admin/products'),
  createProduct: (data: any) => api.post('/admin/products', data),
  updateProduct: (id: number, data: any) => api.put(`/admin/products/${id}`, data),
  deleteProduct: (id: number) => api.delete(`/admin/products/${id}`),
  addCustomField: (id: number, data: any) => api.post(`/admin/products/${id}/custom-fields`, data),

  getOrders: (params?: any) => api.get('/admin/orders', { params }),
  exportOrders: () => api.get('/admin/orders/export'),
  processRefund: (id: number, data: any) => api.put(`/admin/orders/${id}/refund`, data),

  getSuppliers: () => api.get('/admin/suppliers'),
  createSupplier: (data: any) => api.post('/admin/suppliers', data),
  updateSupplier: (id: number, data: any) => api.put(`/admin/suppliers/${id}`, data),
  deleteSupplier: (id: number) => api.delete(`/admin/suppliers/${id}`),
  authorizeSupplier: (id: number, data: any) => api.post(`/admin/suppliers/${id}/authorize`, data),

  getLotteries: () => api.get('/lotteries'),
  createLottery: (data: any) => api.post('/admin/lotteries', data),
  updateLottery: (id: number, data: any) => api.put(`/admin/lotteries/${id}`, data),
  deleteLottery: (id: number) => api.delete(`/admin/lotteries/${id}`),
  addLotteryPrize: (id: number, data: any) => api.post(`/admin/lotteries/${id}/prizes`, data),
  drawLottery: (id: number) => api.post(`/admin/lotteries/${id}/draw`),

  getTickets: (params?: any) => api.get('/admin/tickets', { params }),
  updateTicket: (id: number, data: any) => api.put(`/admin/tickets/${id}`, data),
  assignTicket: (id: number, data: any) => api.put(`/admin/tickets/${id}/assign`, data),

  getSettings: () => api.get('/admin/settings'),
  updateSettings: (data: any) => api.put('/admin/settings', data),
  updateSMTP: (data: any) => api.put('/admin/settings/smtp', data),
  updatePayment: (data: any) => api.put('/admin/settings/payment', data),

  getInventory: () => api.get('/admin/inventory'),

  getArticles: (params?: any) => api.get('/admin/articles', { params }),
  createArticle: (data: any) => api.post('/admin/articles', data),
  updateArticle: (id: number, data: any) => api.put(`/admin/articles/${id}`, data),
  deleteArticle: (id: number) => api.delete(`/admin/articles/${id}`),
  togglePinArticle: (id: number) => api.put(`/admin/articles/${id}/pin`),

  getBanners: () => api.get('/admin/banners'),
  createBanner: (data: any) => api.post('/admin/banners', data),
  updateBanner: (id: number, data: any) => api.put(`/admin/banners/${id}`, data),
  deleteBanner: (id: number) => api.delete(`/admin/banners/${id}`),
  toggleBanner: (id: number) => api.put(`/admin/banners/${id}/toggle`),

  getPaymentChannels: () => api.get('/admin/payment-channels'),
  createPaymentChannel: (data: any) => api.post('/admin/payment-channels', data),
  updatePaymentChannel: (id: number, data: any) => api.put(`/admin/payment-channels/${id}`, data),
  deletePaymentChannel: (id: number) => api.delete(`/admin/payment-channels/${id}`),
  togglePaymentChannel: (id: number) => api.put(`/admin/payment-channels/${id}/toggle`),

  getLogs: (lines?: number) => api.get('/admin/logs', { params: { lines: lines || 500 } }),
  clearLogs: () => api.delete('/admin/logs'),
  getLogInfo: () => api.get('/admin/logs/info'),
}
