import { lotteryRequest } from "@/lib/lottery-http";

export type PlayerAuthCaptchaResponse = {
  captcha_key: string;
  image_base64: string;
};

export type PlayerAuthLoginPayload = {
  site_code?: string;
  username: string;
  password: string;
  captcha_key: string;
  captcha_code: string;
};

export type PlayerAuthLoginData = {
  access_token: string;
  expires_in: number;
  token_type: string;
  player: {
    id: number;
    site_code: string;
    username: string | null;
    nickname: string | null;
    funding_mode: string;
    auth_source: string;
  };
};

/** `GET /api/v1/player/auth/captcha`（公开） */
export function getPlayerAuthCaptcha(): Promise<PlayerAuthCaptchaResponse> {
  return lotteryRequest.get<PlayerAuthCaptchaResponse>(`/player/auth/captcha`, {
    skipGlobalServerError: true,
  });
}

/** `POST /api/v1/player/auth/login`（公开） */
export function postPlayerAuthLogin(body: PlayerAuthLoginPayload): Promise<PlayerAuthLoginData> {
  const siteCode = process.env.NEXT_PUBLIC_PLAYER_SITE_CODE?.trim();
  return lotteryRequest.post<PlayerAuthLoginData>(`/player/auth/login`, {
    ...(siteCode ? { site_code: siteCode } : {}),
    ...body,
  });
}
