import { adminRequest } from "@/lib/admin-http";


import type {
  AdminJackpotContributionsData,
  AdminJackpotPoolAdjustmentsData,
  AdminJackpotPoolAdjustResult,
  AdminJackpotPoolsData,
  AdminJackpotPayoutLogsData,
  AdminJackpotPoolRow,
} from "@/types/api/admin-jackpot";

const A = `/admin`;

export async function getAdminJackpotPools(): Promise<AdminJackpotPoolsData> {
  return adminRequest.get<AdminJackpotPoolsData>(`${A}/jackpot/pools`);
}

export type AdminJackpotPoolUpdateBody = Partial<{
  contribution_rate: number;
  trigger_threshold: number;
  payout_rate: number;
  force_trigger_draw_gap: number;
  min_bet_amount: number;
  combo_trigger_play_codes: string[];
  status: number;
}>;

export async function putAdminJackpotPool(
  poolId: number,
  body: AdminJackpotPoolUpdateBody,
): Promise<AdminJackpotPoolRow> {
  return adminRequest.put<AdminJackpotPoolRow>(`${A}/jackpot/pools/${poolId}`, body);
}

export async function postAdminJackpotPoolAdjustment(
  poolId: number,
  body: { amount_delta: number; reason: string },
): Promise<AdminJackpotPoolAdjustResult> {
  return adminRequest.post<AdminJackpotPoolAdjustResult>(`${A}/jackpot/pools/${poolId}/adjustments`, body);
}

export async function getAdminJackpotPoolAdjustments(
  poolId: number,
  q: { page?: number; per_page?: number } = {},
): Promise<AdminJackpotPoolAdjustmentsData> {
  return adminRequest.get<AdminJackpotPoolAdjustmentsData>(`${A}/jackpot/pools/${poolId}/adjustments`, {
    params: q,
  });
}

export async function postAdminJackpotManualBurst(
  poolId: number,
  body: { draw_id: number | string },
): Promise<{
  current_amount: number;
  burst_amount: number;
  log_id: number | null;
  winner_count: number;
  draw_no: string;
  wallet_credited: boolean;
}> {
  return adminRequest.post(`${A}/jackpot/pools/${poolId}/manual-burst`, body);
}

export type AdminJackpotLogsQuery = {
  page?: number;
  per_page?: number;
  draw_no?: string;
};

export async function getAdminJackpotPayoutLogs(
  q: AdminJackpotLogsQuery = {},
): Promise<AdminJackpotPayoutLogsData> {
  return adminRequest.get<AdminJackpotPayoutLogsData>(`${A}/jackpot/payout-logs`, { params: q });
}

export async function getAdminJackpotContributions(
  q: AdminJackpotLogsQuery = {},
): Promise<AdminJackpotContributionsData> {
  return adminRequest.get<AdminJackpotContributionsData>(`${A}/jackpot/contributions`, { params: q });
}
