export default defineEventHandler((event) => {
  const path = getRequestURL(event).pathname

  // /lire → /economique/modele-eco
  if (path.startsWith('/lire')) {
    const rest = path.slice(5) // remove '/lire'
    return sendRedirect(event, `/economique/modele-eco${rest || '/'}`, 301)
  }

  if (path.startsWith('/ecouter')) {
    const rest = path.slice(8) // remove '/ecouter'
    return sendRedirect(event, `/en-musique${rest || '/'}`, 301)
  }

  if (path === '/autonomie' || path === '/autonomie/') {
    return sendRedirect(event, '/numerique', 301)
  }

  if (path === '/decision' || path === '/decision/') {
    return sendRedirect(event, '/citoyenne/decision', 301)
  }

  // /modele-eco → /economique/modele-eco
  if (path.startsWith('/modele-eco')) {
    const rest = path.slice(11) // remove '/modele-eco'
    return sendRedirect(event, `/economique/modele-eco${rest || '/'}`, 301)
  }

  // Redirect old /gestation/* routes to proper sections
  if (path.startsWith('/gestation/')) {
    const slug = path.slice(11).replace(/\/$/, '')
    const numeriquePages = ['logiciel-libre', 'authentification-wot', 'cloud-libre']
    if (numeriquePages.includes(slug)) {
      return sendRedirect(event, `/numerique/${slug}`, 301)
    }
    if (slug === 'productions-collectives') {
      return sendRedirect(event, '/economique/productions-collectives', 301)
    }
    if (slug === 'tarifs-eau') {
      return sendRedirect(event, '/citoyenne/tarifs-eau', 301)
    }
    // Fallback
    return sendRedirect(event, '/', 301)
  }
})
