import { DREAM_API_KEY } from "$env/static/private";

const path =
  "https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image";

const headers = {
  Accept: "application/json",
  "Content-Type": "application/json",
  Authorization: DREAM_API_KEY,
};
export async function generateImage(prompt: string, negativePrompt: string) {
  const body = {
    steps: 10,
    width: 832,
    height: 1216,
    seed: Math.floor(Math.random() * 100000),
    cfg_scale: 5,
    samples: 1,
    style_preset: "fantasy-art",
    text_prompts: [
      {
        "text": prompt,
        "weight": 1
      },
      {
        "text": negativePrompt,
        "weight": -1
      }
    ],
  };

  const response = await fetch(
    path,
    {
      headers,
      method: "POST",
      body: JSON.stringify(body),
    }
  );

  if (!response.ok) {
    throw new Error(`Non-200 response: ${await response.text()}`)
  }

  const responseJSON = await response.json();

  return responseJSON.artifacts[0];
}
