import { mainClient } from '@/api/clients/main'
import type { IApiResponse } from '@/api/types'
import type { ILoginResponse } from '@/types/user'

/**
 * 验证码登录/注册
 * @param tel 邮箱地址
 * @param code 验证码
 */
export async function loginWithCode(tel: string, code: string) {
  const res = await mainClient.request<ILoginResponse>({
    url: 'book/user/registerOrLogin',
    method: 'GET',
    data: { tel, code }
  })
  return res
}

/**
 * 密码登录
 * @param phone 邮箱地址
 * @param password 密码
 */
export async function loginWithPassword(phone: string, password: string) {
  const res = await mainClient.request<ILoginResponse>({
    url: 'book/user/login',
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: { phone, password }
  })
  return res
}

/**
 * 重置密码
 * @param phone 邮箱地址
 * @param code 验证码
 * @param password 新密码
 */
export async function resetPassword(phone: string, code: string, password: string) {
  const res = await mainClient.request<IApiResponse>({
    url: 'book/user/setPassword',
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: { phone, code, password }
  })
  return res.data
}
