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

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

const getInviteCode = () => Taro.getStorageSync('invite_code') || ''

// Orders
export const createOrder = (data: any) => {
    const code = getInviteCode()
    if (code) data.ref_code = code
    return 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' })
export const queryOrderStatus = (orderId: number) => request({ url: `/orders/${orderId}/query_status/` })

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

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

// Distributor
export const distributorRegister = (data: any) => {
    const code = getInviteCode()
    if (code && !data.invite_code) data.invite_code = code
    return 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 } })
export const distributorTeam = () => request({ url: '/distributor/team/' })
export const distributorEarnings = (params?: any) => request({ url: '/distributor/earnings/', data: params })
export const distributorOrders = (params?: any) => request({ url: '/distributor/orders/', data: params })

// 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 likeTopic = (id: number) => request({ url: `/community/topics/${id}/like/`, method: 'POST' })
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 likeReply = (id: number) => request({ url: `/community/replies/${id}/like/`, method: 'POST' })
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/' })

// Competitions
export const getCompetitions = (params?: any) => request({ url: '/competition/competitions/', data: params })
export const getCompetitionDetail = (id: number) => request({ url: `/competition/competitions/${id}/` })
export const enrollCompetition = (id: number, data: any) => request({ url: `/competition/competitions/${id}/enroll/`, method: 'POST', data })
export const getMyCompetitionEnrollment = (id: number) => request({ url: `/competition/competitions/${id}/my_enrollment/` })
export const getMyEnrollments = () => request({ url: '/competition/competitions/my_enrollments/' })
export const getProjects = (params?: any) => request({ url: '/competition/projects/', data: params })
export const getProjectDetail = (id: number) => request({ url: `/competition/projects/${id}/` })
export const createProject = (data: any) => request({ url: '/competition/projects/', method: 'POST', data })
export const updateProject = (id: number, data: any) => request({ url: `/competition/projects/${id}/`, method: 'PATCH', data })
export const submitProject = (id: number) => request({ url: `/competition/projects/${id}/submit/`, method: 'POST' })
export const getComments = (params: any) => request({ url: '/competition/comments/', data: params })
export const uploadProjectFile = (filePath: string, projectId: number, fileName?: string) => {
    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}/competition/files/`,
        filePath,
        name: 'file',
        formData: {
            project: projectId,
            name: fileName || ''
        },
        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')
    })
}

// 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'

