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


import type {
  AdminIntegrationSiteConnectivityResult,
  AdminIntegrationSiteCreatePayload,
  AdminIntegrationSiteDetail,
  AdminIntegrationSiteListData,
  AdminIntegrationSiteParameterSheet,
  AdminIntegrationSiteUpdatePayload,
  AdminIntegrationSiteSecrets,
  AdminIntegrationSiteWithSecrets,
} from "@/types/api/admin-integration-site";

const A = `/admin`;

export async function getAdminIntegrationSites(): Promise<AdminIntegrationSiteListData> {
  return adminRequest.get<AdminIntegrationSiteListData>(`${A}/integration-sites`);
}

export async function getAdminIntegrationSite(id: number): Promise<AdminIntegrationSiteDetail> {
  return adminRequest.get<AdminIntegrationSiteDetail>(`${A}/integration-sites/${id}`);
}

export async function postAdminIntegrationSite(
  body: AdminIntegrationSiteCreatePayload,
): Promise<AdminIntegrationSiteWithSecrets> {
  return adminRequest.post<AdminIntegrationSiteWithSecrets>(`${A}/integration-sites`, body);
}

export async function putAdminIntegrationSite(
  id: number,
  body: AdminIntegrationSiteUpdatePayload,
): Promise<AdminIntegrationSiteDetail> {
  return adminRequest.put<AdminIntegrationSiteDetail>(`${A}/integration-sites/${id}`, body);
}

export async function deleteAdminIntegrationSite(id: number): Promise<null> {
  return adminRequest.delete<null>(`${A}/integration-sites/${id}`);
}

export async function postAdminIntegrationSiteRotateSecrets(
  id: number,
): Promise<AdminIntegrationSiteWithSecrets> {
  return adminRequest.post<AdminIntegrationSiteWithSecrets>(
    `${A}/integration-sites/${id}/rotate-secrets`,
    {},
  );
}

export async function postAdminIntegrationSiteConnectivityTest(
  id: number,
  body: { site_player_id: string; currency_code?: string },
): Promise<AdminIntegrationSiteConnectivityResult> {
  return adminRequest.post<AdminIntegrationSiteConnectivityResult>(
    `${A}/integration-sites/${id}/connectivity-test`,
    body,
  );
}

export async function getAdminIntegrationSiteExport(
  id: number,
  format: "json" | "csv" = "json",
): Promise<AdminIntegrationSiteParameterSheet> {
  return adminRequest.get<AdminIntegrationSiteParameterSheet>(
    `${A}/integration-sites/${id}/export`,
    { params: { format } },
  );
}

export async function getAdminIntegrationSiteSecrets(
  id: number,
): Promise<AdminIntegrationSiteSecrets> {
  return adminRequest.get<AdminIntegrationSiteSecrets>(`${A}/integration-sites/${id}/secrets`);
}

