import Taro from '@tarojs/taro'

const BASE_URL = (typeof process !== 'undefined' && process.env && process.env.TARO_APP_API_URL) || 'https://market.quant-speed.com/api'

export const request = async (options: Taro.request.Option) => {
  const token = Taro.getStorageSync('token')
  
  const header = {
    'Content-Type': 'application/json',
    ...options.header
  }

  if (token) {
    header['Authorization'] = `Bearer ${token}`
  }

  // Ensure URL is clean
  let url = options.url
  if (!url.startsWith('http')) {
      url = BASE_URL + (url.startsWith('/') ? url : '/' + url)
  }

  try {
    const res = await Taro.request({
      ...options,
      url: url,
      header: header,
    })

    if (res.statusCode === 401) {
      // Token expired
      Taro.removeStorageSync('token')
      // Optional: Auto re-login logic could go here
      Taro.showToast({ title: '登录已过期，请重试', icon: 'none' })
      // For user experience, maybe trigger login
      return Promise.reject(res)
    }

    if (res.statusCode >= 400) {
       const errMsg = res.data?.error || res.data?.detail || '请求失败'
       Taro.showToast({ title: errMsg, icon: 'none' })
       return Promise.reject(res)
    }

    return res.data
  } catch (err) {
    Taro.showToast({ title: '网络错误', icon: 'none' })
    return Promise.reject(err)
  }
}

export const login = async () => {
  try {
    const { code } = await Taro.login()
    if (!code) throw new Error('wx.login failed')
    
    const res = await Taro.request({
      url: `${BASE_URL}/wechat/login/`,
      method: 'POST',
      data: { code }
    })
    
    if (res.statusCode === 200 && res.data.token) {
      Taro.setStorageSync('token', res.data.token)
      Taro.setStorageSync('openid', res.data.openid)
      // Save other info if needed
      return res.data
    } else {
      throw new Error(res.data.error || 'Login failed')
    }
  } catch (err) {
    console.error('Login error', err)
    // Don't toast here to avoid spamming if called automatically
    throw err
  }
}
