export async function fetchFromAPI(endpoint: string, options: RequestInit = {}) {
  const apiKey = process.env.NEXT_PUBLIC_CF_API_KEY || localStorage.getItem("cf_api_key")

  const response = await fetch(
    `${process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001"}${endpoint}`,
    {
      ...options,
      headers: {
        "Content-Type": "application/json",
        "x-api-key": apiKey || "",
        ...options.headers,
      },
    },
  )

  if (!response.ok) {
    const error = await response.json()
    throw new Error(error.message || "API request failed")
  }

  return response.json()
}
