import { post } from '@/utils/request'

const TOKEN_KEY = 'smarttrade_platform_token'

export function getToken() {
  return localStorage.getItem(TOKEN_KEY) || ''
}

export function setToken(token) {
  localStorage.setItem(TOKEN_KEY, token)
}

export function removeToken() {
  localStorage.removeItem(TOKEN_KEY)
}

export async function login({ account, password }) {
  const response = await post(
    '/router/rest/sys/auth/login',
    { account, password },
    { headers: { isToken: false } }
  )
  const token = response?.token || response?.result?.token || response?.accessToken || response?.data?.accessToken
  if (!token) {
    throw new Error('登录失败：未获取到 token')
  }

  setToken(token)
  return response
}

export function logout() {
  removeToken()
}
