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


import type {
  AdminPlayTypeRow,
  AdminPlayTypesData,
  ConfigVersionListData,
  OddsVersionDetail,
  PlayConfigVersionDetail,
  RiskCapVersionDetail,
} from "@/types/api/admin-config";

const A = `/admin`;

type ConfigVersionListParams = {
  status?: string;
  page?: number;
  per_page?: number;
};

export async function getAdminPlayTypes(): Promise<AdminPlayTypesData> {
  return adminRequest.get<AdminPlayTypesData>(`${A}/play-types`);
}

export async function patchAdminPlayType(
  playCode: string,
  body: Partial<{
    is_enabled: boolean;
    sort_order: number;
    display_name: string | null;
    supports_multi_number: boolean;
    reserved_rule_json: unknown;
  }>,
): Promise<AdminPlayTypeRow> {
  return adminRequest.patch(`${A}/play-types/${encodeURIComponent(playCode)}`, body);
}

export async function getPlayConfigVersions(params?: {
  status?: string;
  page?: number;
  per_page?: number;
}): Promise<ConfigVersionListData> {
  return adminRequest.get(`${A}/config/play-versions`, { params });
}

export async function getPlayConfigVersion(id: number): Promise<PlayConfigVersionDetail> {
  return adminRequest.get(`${A}/config/play-versions/${id}`);
}

export async function postPlayConfigVersion(body?: {
  reason?: string | null;
  clone_from_version_id?: number | null;
}): Promise<PlayConfigVersionDetail> {
  return adminRequest.post(`${A}/config/play-versions`, body ?? {});
}

export async function putPlayConfigItems(
  id: number,
  items: Array<{
    play_code: string;
    category: string | null;
    dimension: number | null;
    bet_mode: string | null;
    display_name: string;
    is_enabled?: boolean;
    min_bet_amount: number;
    max_bet_amount: number;
    display_order?: number;
    supports_multi_number?: boolean;
    reserved_rule_json?: unknown;
    rule_text_zh?: string | null;
    rule_text_en?: string | null;
    rule_text_ne?: string | null;
    extra_config_json?: unknown;
  }>,
): Promise<PlayConfigVersionDetail> {
  return adminRequest.put(`${A}/config/play-versions/${id}/items`, { items });
}

export async function publishPlayConfigVersion(id: number): Promise<PlayConfigVersionDetail> {
  return adminRequest.post(`${A}/config/play-versions/${id}/publish`);
}

export async function deletePlayConfigVersion(id: number): Promise<{ deleted: boolean }> {
  return adminRequest.delete(`${A}/config/play-versions/${id}`);
}

export async function getOddsVersions(params?: {
  status?: string;
  page?: number;
  per_page?: number;
}): Promise<ConfigVersionListData> {
  return adminRequest.get(`${A}/config/odds-versions`, { params });
}

export async function getOddsVersion(id: number): Promise<OddsVersionDetail> {
  return adminRequest.get(`${A}/config/odds-versions/${id}`);
}

export async function postOddsVersion(body?: {
  reason?: string | null;
  clone_from_version_id?: number | null;
}): Promise<OddsVersionDetail> {
  return adminRequest.post(`${A}/config/odds-versions`, body ?? {});
}

export async function putOddsItems(
  id: number,
  items: Array<{
    play_code: string;
    prize_scope: string;
    odds_value: number;
    rebate_rate?: number;
    commission_rate?: number;
    currency_code: string;
    extra_config_json?: unknown;
  }>,
): Promise<OddsVersionDetail> {
  return adminRequest.put(`${A}/config/odds-versions/${id}/items`, { items });
}

export async function publishOddsVersion(id: number): Promise<OddsVersionDetail> {
  return adminRequest.post(`${A}/config/odds-versions/${id}/publish`);
}

export async function deleteOddsVersion(id: number): Promise<{ deleted: boolean }> {
  return adminRequest.delete(`${A}/config/odds-versions/${id}`);
}

export async function getRiskCapVersions(params?: {
  status?: string;
  page?: number;
  per_page?: number;
}): Promise<ConfigVersionListData> {
  return adminRequest.get(`${A}/config/risk-cap-versions`, { params });
}

export async function getAllConfigVersions(
  fetchPage: (params?: ConfigVersionListParams) => Promise<ConfigVersionListData>,
  params?: Omit<ConfigVersionListParams, "page" | "per_page">,
): Promise<ConfigVersionListData> {
  const items: ConfigVersionListData["items"] = [];
  let meta: ConfigVersionListData["meta"] | null = null;

  for (let page = 1; ; page += 1) {
    const data = await fetchPage({ ...params, page, per_page: 100 });
    items.push(...data.items);
    meta = data.meta;
    if (page >= data.meta.last_page) {
      break;
    }
  }

  return {
    items,
    meta: meta ?? {
      current_page: 1,
      per_page: 0,
      total: 0,
      last_page: 1,
    },
  };
}

export async function getRiskCapVersion(id: number): Promise<RiskCapVersionDetail> {
  return adminRequest.get(`${A}/config/risk-cap-versions/${id}`);
}

export async function postRiskCapVersion(body?: {
  reason?: string | null;
  clone_from_version_id?: number | null;
}): Promise<RiskCapVersionDetail> {
  return adminRequest.post(`${A}/config/risk-cap-versions`, body ?? {});
}

export async function putRiskCapItems(
  id: number,
  items: Array<{
    draw_id?: number | null;
    normalized_number: string;
    cap_amount: number;
    cap_type: string;
  }>,
): Promise<RiskCapVersionDetail> {
  return adminRequest.put(`${A}/config/risk-cap-versions/${id}/items`, { items });
}

export async function publishRiskCapVersion(id: number): Promise<RiskCapVersionDetail> {
  return adminRequest.post(`${A}/config/risk-cap-versions/${id}/publish`);
}

export async function deleteRiskCapVersion(id: number): Promise<{ deleted: boolean }> {
  return adminRequest.delete(`${A}/config/risk-cap-versions/${id}`);
}
