import { request } from './request'

export async function logout() {
  await request('/mobile/user/logout', { method: 'POST' })
}

export async function fetchUserSummary() {
  const res = await request<{ wishCount: number; receiveCount: number }>(
    '/mobile/user/wish/summary',
    { method: 'POST' },
  )
  return res.data
}

export async function getCaptcha() {
  const res = await request<{ key: string; value: string }>('/mobile/user/login/captcha')
  return res.data
}

export async function sendSms(phone: string) {
  // 后端暂无发送短信接口，仅做前端倒计时模拟
  console.log(`[Mock] Sending SMS to ${phone}`)
  return Promise.resolve()
}

export async function login(phone: string, code: string, captcha: string, captchaKey: string) {
  const res = await request<{ token: string; mobile: string; nickname: string; avatar: string; isAdmin: number }>(
    '/mobile/user/login',
    {
      method: 'POST',
      body: JSON.stringify({ mobile: phone, code, captcha, captchaKey }),
    },
  )
  return {
    token: res.data.token,
    phone: res.data.mobile,
    nickname: res.data.nickname,
    avatarUrl: res.data.avatar || '',
    isAdmin: res.data.isAdmin || 0,
  }
}
