/**
 * VM112 onboarding API client — Fase B/C
 */
export type Vm112Config = { baseUrl: string; sessionId: string };

export async function vm112Post(
  cfg: Vm112Config,
  path: string,
  body: Record<string, unknown>,
): Promise<Record<string, unknown>> {
  const res = await fetch(`${cfg.baseUrl}${path}`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-Onboarding-Session": cfg.sessionId,
    },
    body: JSON.stringify(body),
  });
  const data = (await res.json().catch(() => ({}))) as Record<string, unknown>;
  if (!res.ok) {
    const msg = (data.detail as string) || (data.message as string) || res.statusText;
    throw new Error(typeof msg === "string" ? msg : JSON.stringify(msg));
  }
  return data;
}

export async function vm112Get(
  cfg: Vm112Config,
  path: string,
): Promise<Record<string, unknown>> {
  const res = await fetch(`${cfg.baseUrl}${path}`, {
    headers: { "X-Onboarding-Session": cfg.sessionId },
  });
  const data = (await res.json().catch(() => ({}))) as Record<string, unknown>;
  if (!res.ok) throw new Error(res.statusText);
  return data;
}
