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


import type { AdminDrawFinanceSummaryData } from "@/types/api/admin-draw-finance";
import type {
  AdminDrawBatchesData,
  AdminDrawActionResponse,
  AdminDrawListData,
  AdminDrawBatchCreateResponse,
  AdminDrawManualBatchPayload,
  AdminDrawPublishResponse,
  AdminDrawPlanGenerateResponse,
  AdminDrawShowData,
  AdminDrawCreatePayload,
  AdminDrawCreateResponse,
} from "@/types/api/admin-draws";

const A = `/admin`;

export type AdminDrawListQuery = {
  page?: number;
  per_page?: number;
  draw_no?: string;
  status?: string;
  agent_node_id?: number;
};

export async function getAdminDraws(q: AdminDrawListQuery = {}): Promise<AdminDrawListData> {
  return adminRequest.get<AdminDrawListData>(`${A}/draws`, { params: q });
}

export async function getAdminDraw(drawId: number): Promise<AdminDrawShowData> {
  return adminRequest.get<AdminDrawShowData>(`${A}/draws/${drawId}`);
}

export async function getAdminDrawResultBatches(drawId: number): Promise<AdminDrawBatchesData> {
  return adminRequest.get<AdminDrawBatchesData>(`${A}/draws/${drawId}/result-batches`);
}

export async function getAdminDrawFinanceSummary(
  drawId: number,
): Promise<AdminDrawFinanceSummaryData> {
  return adminRequest.get<AdminDrawFinanceSummaryData>(
    `${A}/draws/${drawId}/finance-summary`,
  );
}

export async function postAdminPublishResultBatch(
  drawId: number,
  batchId: number,
): Promise<AdminDrawPublishResponse> {
  return adminRequest.post<AdminDrawPublishResponse>(
    `${A}/draws/${drawId}/result-batches/${batchId}/publish`,
  );
}

export async function deleteAdminPendingResultBatch(
  drawId: number,
  batchId: number,
): Promise<AdminDrawActionResponse> {
  return adminRequest.delete<AdminDrawActionResponse>(
    `${A}/draws/${drawId}/result-batches/${batchId}`,
  );
}

export async function postAdminGenerateDrawPlan(): Promise<AdminDrawPlanGenerateResponse> {
  return adminRequest.post<AdminDrawPlanGenerateResponse>(`${A}/draws/generate-plan`);
}

export async function postAdminCreateDraw(
  payload: AdminDrawCreatePayload,
): Promise<AdminDrawCreateResponse> {
  return adminRequest.post<AdminDrawCreateResponse>(`${A}/draws`, payload);
}

export async function putAdminUpdateDraw(
  drawId: number,
  payload: AdminDrawCreatePayload,
): Promise<AdminDrawCreateResponse> {
  return adminRequest.put<AdminDrawCreateResponse>(`${A}/draws/${drawId}`, payload);
}

export async function deleteAdminDraw(drawId: number): Promise<{ deleted: boolean }> {
  return adminRequest.delete<{ deleted: boolean }>(`${A}/draws/${drawId}`);
}

export async function postAdminBatchDestroyDraws(
  drawIds: number[],
): Promise<{ success: number[]; failed: Array<{ id: number; reason: string }> }> {
  return adminRequest.post<{ success: number[]; failed: Array<{ id: number; reason: string }> }>(
    `${A}/draws/batch-destroy`,
    { draw_ids: drawIds },
  );
}

export async function postAdminManualCloseDraw(drawId: number): Promise<AdminDrawActionResponse> {
  return adminRequest.post<AdminDrawActionResponse>(`${A}/draws/${drawId}/manual-close`);
}

export async function postAdminCancelDraw(drawId: number): Promise<AdminDrawActionResponse> {
  return adminRequest.post<AdminDrawActionResponse>(`${A}/draws/${drawId}/cancel`);
}

export async function postAdminRunDrawRng(drawId: number): Promise<AdminDrawBatchCreateResponse> {
  return adminRequest.post<AdminDrawBatchCreateResponse>(`${A}/draws/${drawId}/rng`);
}

export async function postAdminCreateManualResultBatch(
  drawId: number,
  payload: AdminDrawManualBatchPayload,
): Promise<AdminDrawBatchCreateResponse> {
  return adminRequest.post<AdminDrawBatchCreateResponse>(
    `${A}/draws/${drawId}/result-batches`,
    payload,
  );
}

export async function postAdminReopenDraw(drawId: number): Promise<AdminDrawActionResponse> {
  return adminRequest.post<AdminDrawActionResponse>(`${A}/draws/${drawId}/reopen`);
}
