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

type Captcha = CaptchaResult | null

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

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

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

export async function adminMe() {
  const { data } = await adminClient.get<ApiResponse<User>>("/v1/admin/auth/me")
  return data
}
