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

type Captcha = CaptchaResult | null

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

export async function changePassword(payload: { old_password?: string; new_password: string }) {
  const { data } = await client.put<ApiResponse<User>>("/v1/users/me/password", payload)
  return data
}

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

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

export async function bindEmail(email: string, code: string) {
  const { data } = await client.post<ApiResponse<User>>("/v1/users/me/bind-email", { email, code })
  return data
}

export async function bindPhone(phone: string, code: string) {
  const { data } = await client.post<ApiResponse<User>>("/v1/users/me/bind-phone", { phone, code })
  return data
}
