export default defineEventHandler(async (event) => {
  const params = event.context.params as { designId?: string }
  const designId = params?.designId

  if (!designId) {
    throw createError({ statusCode: 400, statusMessage: "Missing design id" })
  }

  const config = useRuntimeConfig()
  const backendUrl = config.public?.backendUrl

  if (!backendUrl) {
    throw createError({ statusCode: 500, statusMessage: "Backend URL not configured" })
  }

  try {
    const design = await $fetch(`/designs/${encodeURIComponent(designId)}`, {
      baseURL: backendUrl,
      method: "GET",
    })

    return design
  } catch (err) {
    console.error(`[designs] Failed to fetch design ${designId}`, err)
    throw createError({
      statusCode: 502,
      statusMessage: (err as Error)?.message ?? "Failed to load design",
    })
  }
})
