export type ReportFilterSnapshot = {
  dateFrom: string;
  dateTo: string;
  playerId: number | null;
  play: string;
  operatorId: number | null;
  drawId: number | null;
  drawNo: string;
  number: string;
};

/** UI report keys used in reports-console */
export type ReportUiKey =
  | "draw_profit"
  | "daily_profit"
  | "player_win_loss"
  | "player_transfer"
  | "hot_number_risk"
  | "play_dimension"
  | "sold_out_number"
  | "admin_audit";

/** Maps UI keys to POST /admin/report-jobs `report_type` */
export const REPORT_UI_TO_JOB_TYPE: Record<ReportUiKey, string> = {
  draw_profit: "draw_profit_summary",
  daily_profit: "daily_profit_summary",
  player_win_loss: "player_win_loss",
  player_transfer: "wallet_transfer_report",
  hot_number_risk: "hot_number_risk_report",
  play_dimension: "play_dimension_report",
  sold_out_number: "sold_out_number_report",
  admin_audit: "audit_operation_report",
};

export function buildReportJobParameters(
  key: ReportUiKey,
  filters: ReportFilterSnapshot,
): Record<string, unknown> {
  const params: Record<string, unknown> = {};

  if (filters.dateFrom) {
    params.date_from = filters.dateFrom;
  }
  if (filters.dateTo) {
    params.date_to = filters.dateTo;
  }
  if (filters.playerId != null && filters.playerId > 0) {
    params.player_id = filters.playerId;
  }
  if (filters.operatorId != null && filters.operatorId > 0) {
    params.operator_id = filters.operatorId;
  }
  if (filters.play.trim()) {
    params.play_code = filters.play.trim();
  }

  if (key === "draw_profit" || key === "hot_number_risk" || key === "sold_out_number") {
    if (filters.drawId != null && filters.drawId > 0) {
      params.draw_id = filters.drawId;
    }
    if (filters.drawNo.trim()) {
      params.draw_no = filters.drawNo.trim();
    }
  }

  if (key === "hot_number_risk" && filters.number.trim()) {
    const digits = filters.number.trim();
    if (/^\d{4}$/.test(digits)) {
      params.normalized_number = digits;
    }
  }

  return params;
}
