import request from '@/utils/request'

interface LoginParamsType {
  name?: string // 姓名
  email: string // 邮箱
  password?: string // 密码
  operationType: 'REGISTER' | 'LOGIN' | 'FORGET_PWD'
  verifyCode?: string // 验证码
}

// 发送验证码
export const precheckEmail = (params: { email: string }): Promise<ApiResponse> => {
  return request({
    url: '/api/auth/precheckEmail',
    method: 'get',
    params
  })
}

export const fetchRegisterOrLogin = (data: LoginParamsType): Promise<LoginResponse> => {
  return request({
    url: '/api/auth/registerOrLogin',
    method: 'post',
    data
  })
}

export const resetPassword = (data: LoginParamsType): Promise<ApiResponse> => {
  return request({
    url: '/api/auth/forgotPwd',
    method: 'post',
    data
  })
}

export const checkLoginStatus = (): Promise<ApiResponse<LoginResponse>> => {
  return request({
    url: '/api/auth/checkLoginStatus',
    method: 'get',
    meta: { responseAll: true }
  })
}

export const LogOut = (): Promise<ApiResponse> => {
  return request({
    url: '/api/auth/logout',
    method: 'get'
  })
}

// Google登录/注册参数类型
interface GoogleAuthParamsType {
  accessToken?: string // Google ID Token (用于One Tap登录)
}
export const googleAuth = (data: GoogleAuthParamsType): Promise<LoginResponse> => {
  return request({
    url: '/api/auth/parseGoogleAccessToken',
    method: 'get',
    params: data
  })
}
