export default defineEventHandler(async (event) => {
  // Vérif cookie admin
  const adminCookie = getCookie(event, 'codev_admin')
  if (adminCookie !== 'ok') {
    throw createError({ statusCode: 403, statusMessage: 'Accès refusé' })
  }

  const config = useRuntimeConfig()
  const tableId = config.codevTableId
  const id = getRouterParam(event, 'id')

  if (!tableId || !id) {
    throw createError({ statusCode: 400, message: 'Parametre manquant' })
  }

  await $fetch(`${config.nocodbUrl}/api/v2/tables/${tableId}/records`, {
    method: 'DELETE',
    headers: { 'xc-token': config.nocodbToken, 'Content-Type': 'application/json' },
    body: JSON.stringify({ Id: Number(id) }),
  }).catch(() => {
    throw createError({ statusCode: 502, statusMessage: 'Erreur suppression' })
  })

  return { status: 200, ok: true }
})
