import { apiFetch } from "./auth";

export interface WorldSummary {
  name: string;
  path: string;
  sizeBytes: string;
  hasLevelDat: boolean;
}

export interface WorldInfoResponse {
  activeWorldName: string;
  worlds: WorldSummary[];
}

export async function getWorldInfo(serverId: string): Promise<WorldInfoResponse> {
  return apiFetch(`/servers/${serverId}/worlds`);
}

export async function initiateWorldUpload(
  serverId: string,
  worldName: string,
): Promise<{
  uploadId: string;
  uploadUrl: string;
  expiresAt: string;
}> {
  return apiFetch(`/servers/${serverId}/worlds/uploads`, {
    method: "POST",
    body: JSON.stringify({ worldName }),
  });
}

export async function completeWorldUpload(
  serverId: string,
  uploadId: string,
): Promise<unknown> {
  return apiFetch(`/servers/${serverId}/worlds/uploads/${uploadId}/complete`, {
    method: "POST",
  });
}

export async function requestWorldDownload(
  serverId: string,
): Promise<{ backupId: string }> {
  return apiFetch(`/servers/${serverId}/worlds/download`, {
    method: "POST",
  });
}
