export async function uploadToOpeninary(file: File, folder: string = "uploads") {
  const formData = new FormData();
  formData.append("files", file);
  formData.append("folder", folder);

  const res = await fetch(`${process.env.OPENINARY_API_URL}/api/upload`, {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.OPENINARY_API_KEY}` },
    body: formData,
  });

  if (!res.ok) {
    const text = await res.text();
    console.error("Openinary upload error:", text);
    throw new Error("Upload başarısız");
  }
  
  const data = await res.json();
  return data.files[0];
}

export async function uploadImage(file: File): Promise<string> {
  const fileData = await uploadToOpeninary(file, "moybeach");
  return `${process.env.NEXT_PUBLIC_OPENINARY_URL}${fileData.url}`;
}
