import client from "./client"
import type { ApiResponse, LoginData, RegisterData, TokenResponse, User } from "../types"
import type { CaptchaResult } from "../utils/geetest"

type Captcha = CaptchaResult | null

export async function sendRegisterCode(email: string, captcha: Captcha) {
  const { data } = await client.post<ApiResponse<{ cooldown: number }>>(
    "/v1/auth/send-code",
    { email, captcha },
  )
  return data
}

export async function sendLoginCode(email: string, captcha: Captcha) {
  const { data } = await client.post<ApiResponse<{ cooldown: number }>>(
    "/v1/auth/send-login-code",
    { email, captcha },
  )
  return data
}

export async function sendRegisterSmsCode(phone: string, captcha: Captcha) {
  const { data } = await client.post<ApiResponse<{ cooldown: number }>>(
    "/v1/auth/send-sms-code",
    { phone, captcha },
  )
  return data
}

export async function sendLoginSmsCode(phone: string, captcha: Captcha) {
  const { data } = await client.post<ApiResponse<{ cooldown: number }>>(
    "/v1/auth/send-login-sms-code",
    { phone, captcha },
  )
  return data
}

export async function register(
  email: string,
  username: string,
  code: string,
  password: string | undefined,
  captcha: Captcha,
  inviteCode?: string,
) {
  const payload: Record<string, unknown> = { email, username, code, captcha }
  if (password) payload.password = password
  if (inviteCode) payload.invite_code = inviteCode
  const { data } = await client.post<ApiResponse<RegisterData>>("/v1/auth/register", payload)
  return data
}

export async function registerBySms(
  phone: string,
  username: string,
  code: string,
  password: string | undefined,
  captcha: Captcha,
  inviteCode?: string,
) {
  const payload: Record<string, unknown> = { phone, username, code, captcha }
  if (password) payload.password = password
  if (inviteCode) payload.invite_code = inviteCode
  const { data } = await client.post<ApiResponse<RegisterData>>("/v1/auth/register-by-sms", payload)
  return data
}

export async function login(email: string, password: string, captcha: Captcha) {
  const { data } = await client.post<ApiResponse<LoginData>>("/v1/auth/login", {
    email,
    password,
    captcha,
  })
  return data
}

export async function loginByCode(email: string, code: string, captcha: Captcha) {
  const { data } = await client.post<ApiResponse<LoginData>>("/v1/auth/login-by-code", {
    email,
    code,
    captcha,
  })
  return data
}

export async function loginBySms(phone: string, code: string, captcha: Captcha) {
  const { data } = await client.post<ApiResponse<LoginData>>("/v1/auth/login-by-sms", {
    phone,
    code,
    captcha,
  })
  return data
}

export async function refreshToken(refresh_token: string) {
  const { data } = await client.post<ApiResponse<TokenResponse>>("/v1/auth/refresh", {
    refresh_token,
  })
  return data
}

export async function logout(refresh_token: string) {
  const { data } = await client.post<ApiResponse<null>>("/v1/auth/logout", {
    refresh_token,
  })
  return data
}

export async function getMe() {
  const { data } = await client.get<ApiResponse<User>>("/v1/users/me")
  return data
}

export async function updateMe(updates: { nickname?: string; avatar?: string }) {
  const { data } = await client.patch<ApiResponse<User>>("/v1/users/me", updates)
  return data
}
