import { createError, getRouterParam } from "h3"
import { createStrapiProxyHandler } from "~~/server/utils/strapiEndpoint"

const COLLECTION_MAP = {
  artistes: "/api/artistes-ondifs",
  artistesinvitees: "/api/artistes-invites",
  concerts: "/api/concerts",
  missions: "/api/mission",
  mission: "/api/mission",
  scolaires: "/api/scolaire",
  valeurs: "/api/valeurs",
  projet: "/api/projet",
  entreprise: "/api/nous-soutenir-entreprise",
  particulier: "/api/nous-soutenir-particulier",
  enfance: "/api/enfance",
  chantons: "/api/chantons",
  academie: "/api/academie",
  insertion: "/api/grandes-ecoles",
  hopital: "/api/hopital",
  articles_mag: "/api/mags",
  "pro-programmer": "/api/pro-programmer",
  "parametres": "/api/parametres",
  instruments: "/api/parc-instruments",
  "home-partout-pour-tous": "/api/home-partout-pour-tous",
  "home-tous-orchestre": "/api/home-tous-orchestre",
  actualites: "/api/actualites",
}

export default defineEventHandler(async (event) => {
  const collection = getRouterParam(event, "collection")
  const strapiPath = COLLECTION_MAP[collection]

  if (!strapiPath) {
    throw createError({
      statusCode: 404,
      statusMessage: "Unknown Strapi collection",
    })
  }

  return createStrapiProxyHandler({ strapiPath })(event)
})
