const EMPTY = { messages: [] as any[] }

export default defineEventHandler(async (event) => {
  const id = Number(getRouterParam(event, 'id'))

  if (!id || isNaN(id)) {
    throw createError({ statusCode: 400, statusMessage: 'ID invalide' })
  }

  const data = await readDataYaml<{ messages: any[] }>('messages.yml', EMPTY)

  const index = data.messages.findIndex(m => m.id === id)
  if (index === -1) {
    throw createError({ statusCode: 404, statusMessage: 'Message non trouvé' })
  }

  data.messages.splice(index, 1)
  await writeDataYaml('messages.yml', data)

  return { ok: true }
})
