// Redirige vers /login toute page visitée sans session valide.
export default defineNuxtRouteMiddleware(async (to) => {
  if (to.path === '/login' || to.path.startsWith('/print') || to.path.startsWith('/view')) return
  try {
    const { authenticated } = await $fetch<{ authenticated: boolean }>('/api/auth/me', {
      headers: import.meta.server ? useRequestHeaders(['cookie']) : undefined,
    })
    if (!authenticated) return navigateTo('/login')
  } catch {
    return navigateTo('/login')
  }
})
