import { request } from '../utils/request'

// Configs / Products
export const getConfigs = () => request({ url: '/configs/' })
export const getConfigDetail = (id: number) => request({ url: `/configs/${id}/` })

// Orders
export const createOrder = (data: any) => request({ url: '/orders/', method: 'POST', data })
export const getOrder = (id: number) => request({ url: `/orders/${id}/` })
export const getMyOrders = () => request({ url: '/orders/' }) 
export const prepayMiniprogram = (orderId: number) => request({ url: `/orders/${orderId}/prepay_miniprogram/`, method: 'POST' })

// AI Services
export const getServices = () => request({ url: '/services/' })
export const getServiceDetail = (id: number) => request({ url: `/services/${id}/` })
export const createServiceOrder = (data: any) => request({ url: '/service-orders/', method: 'POST', data })

// VB Courses
export const getVBCourses = () => request({ url: '/courses/' })
export const getVBCourseDetail = (id: number) => request({ url: `/courses/${id}/` })

// Distributor
export const distributorRegister = (data: any) => request({ url: '/distributor/register/', method: 'POST', data })
export const distributorInfo = () => request({ url: '/distributor/info/' })
export const distributorInvite = () => request({ url: '/distributor/invite/', method: 'POST' })
export const distributorWithdraw = (amount: number) => request({ url: '/distributor/withdraw/', method: 'POST', data: { amount } })
// TODO: Verify if these exist in the API docs
// export const distributorTeam = () => request({ url: '/distributor/team/' })
// export const distributorHistory = () => request({ url: '/distributor/history/' })

// User
export const updateUserInfo = (data: any) => request({ url: '/wechat/update/', method: 'POST', data })
export const wechatLogin = (code: string) => request({ url: '/wechat/login/', method: 'POST', data: { code } })

// Forum / Community
export const getTopics = (params: any) => request({ url: '/community/topics/', data: params })
export const getTopicDetail = (id: number) => request({ url: `/community/topics/${id}/` })
export const createTopic = (data: any) => request({ url: '/community/topics/', method: 'POST', data })
export const updateTopic = (id: number, data: any) => request({ url: `/community/topics/${id}/`, method: 'PATCH', data })
export const getReplies = (params: any) => request({ url: '/community/replies/', data: params })
export const createReply = (data: any) => request({ url: '/community/replies/', method: 'POST', data })
export const updateReply = (id: number, data: any) => request({ url: `/community/replies/${id}/`, method: 'PATCH', data })
export const deleteReply = (id: number) => request({ url: `/community/replies/${id}/`, method: 'DELETE' })
export const deleteTopic = (id: number) => request({ url: `/community/topics/${id}/`, method: 'DELETE' })
export const getStarUsers = () => request({ url: '/users/stars/' })
export const getAnnouncements = () => request({ url: '/community/announcements/' })

// Activities
export const getActivities = () => request({ url: '/community/activities/', data: { _t: Date.now() } })
export const getActivityDetail = (id: number) => request({ url: `/community/activities/${id}/`, data: { _t: Date.now() } })
export const signupActivity = (id: number, data?: any) => request({ url: `/community/activities/${id}/signup/`, method: 'POST', data })
export const getMySignups = () => request({ url: '/community/activities/my_signups/' })

// Upload Media for Forum
export const uploadMedia = (filePath: string, type: 'image' | 'video') => {
  const BASE_URL = (typeof process !== 'undefined' && process.env && process.env.TARO_APP_API_URL) || 'https://market.quant-speed.com/api'
  return Taro.uploadFile({
    url: `${BASE_URL}/community/media/`,
    filePath,
    name: 'file',
    formData: {
      media_type: type
    },
    header: {
      'Authorization': `Bearer ${Taro.getStorageSync('token')}`
    }
  }).then(res => {
    if (res.statusCode >= 200 && res.statusCode < 300) {
      return JSON.parse(res.data)
    }
    throw new Error('Upload failed')
  })
}

import Taro from '@tarojs/taro'

