import { lotteryRequest } from "@/lib/lottery-http";
import type { DrawCurrentResponse } from "@/types/api/draw-current";
import type {
  DrawResultDetailPayload,
  DrawResultsListPayload,
} from "@/types/api/draw-results";

export type GetDrawCurrentParams = {
  /** 与 `play/effective` 一致，决定嵌入快照中的 jackpot 币种 */
  currency?: string;
};

/** `GET /api/v1/draw/current`（无需登录；无当前期时 `data` 为 `null`） */
export function getDrawCurrent(
  params?: GetDrawCurrentParams,
): Promise<DrawCurrentResponse> {
  return lotteryRequest.get<DrawCurrentResponse>(
    `/draw/current`,
    { params: params?.currency ? { currency: params.currency } : undefined },
  );
}

export type GetDrawResultsParams = {
  page?: number;
  /** 与后端 `size` / `per_page` 对齐 */
  size?: number;
  /** `YYYY-MM-DD`，按业务日过滤 */
  business_date?: string;
  currency?: string;
};

/** `GET /api/v1/draw/results` */
export function getDrawResults(
  params?: GetDrawResultsParams,
): Promise<DrawResultsListPayload> {
  return lotteryRequest.get<DrawResultsListPayload>(
    `/draw/results`,
    {
      params: {
        page: params?.page,
        size: params?.size,
        business_date: params?.business_date,
        currency: params?.currency,
      },
    },
  );
}

export type GetDrawResultByNoParams = {
  currency?: string;
};

/** `GET /api/v1/draw/results/{draw_no}` */
export function getDrawResultByNo(
  drawNo: string,
  params?: GetDrawResultByNoParams,
): Promise<DrawResultDetailPayload> {
  const encoded = encodeURIComponent(drawNo);
  return lotteryRequest.get<DrawResultDetailPayload>(
    `/draw/results/${encoded}`,
    { params: params?.currency ? { currency: params.currency } : undefined },
  );
}
