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


import type {
  AdminRiskPoolListData,
  AdminRiskPoolRow,
  AdminRiskPoolShowData,
} from "@/types/api/admin-risk";

const A = `/admin`;

export type AdminRiskPoolListQuery = {
  page?: number;
  per_page?: number;
  sold_out_only?: boolean;
  high_risk_only?: boolean;
  active_only?: boolean;
  normalized_number?: string;
  sort?: "usage_desc" | "locked_desc" | "remaining_asc" | "number_asc";
};

export async function getAdminRiskPools(
  drawId: number,
  q: AdminRiskPoolListQuery = {},
): Promise<AdminRiskPoolListData> {
  return adminRequest.get<AdminRiskPoolListData>(`${A}/draws/${drawId}/risk-pools`, {
    params: {
      page: q.page,
      per_page: q.per_page,
      sold_out_only: q.sold_out_only === true ? 1 : undefined,
      high_risk_only: q.high_risk_only === true ? 1 : undefined,
      active_only: q.active_only === true ? 1 : undefined,
      normalized_number: q.normalized_number || undefined,
      sort: q.sort,
    },
  });
}

export async function postAdminRiskPoolManualClose(
  drawId: number,
  number4d: string,
): Promise<AdminRiskPoolRow> {
  const encoded = encodeURIComponent(number4d);
  return adminRequest.post<AdminRiskPoolRow>(
    `${A}/draws/${drawId}/risk-pools/${encoded}/manual-close`,
  );
}

export async function postAdminRiskPoolRecover(
  drawId: number,
  number4d: string,
): Promise<AdminRiskPoolRow> {
  const encoded = encodeURIComponent(number4d);
  return adminRequest.post<AdminRiskPoolRow>(
    `${A}/draws/${drawId}/risk-pools/${encoded}/recover`,
  );
}

export type AdminRiskPoolShowQuery = {
  page?: number;
  per_page?: number;
};

export async function getAdminRiskPoolDetail(
  drawId: number,
  number4d: string,
  q: AdminRiskPoolShowQuery = {},
): Promise<AdminRiskPoolShowData> {
  const encoded = encodeURIComponent(number4d);
  return adminRequest.get<AdminRiskPoolShowData>(
    `${A}/draws/${drawId}/risk-pools/${encoded}`,
    { params: { page: q.page, per_page: q.per_page } },
  );
}
