// Holding-page gate. While NUXT_PUBLIC_COMING_SOON=true, every route is
// redirected to the locale-appropriate /coming-soon page so the unfinished
// site stays hidden. Flip the env to false (in Coolify) to launch — no code
// change. The team can preview the real site via ?preview=<token>, which sets
// a short-lived cookie so subsequent navigation stays unlocked.
export default defineNuxtRouteMiddleware((to) => {
  const config = useRuntimeConfig()
  if (!config.public.comingSoon) return

  // Never redirect the holding page itself (avoids a loop).
  if (to.path === '/coming-soon' || to.path === '/da/coming-soon') return

  const token = config.public.previewToken as string
  const bypass = useCookie<string | null>('dz_preview', {
    maxAge: 60 * 60 * 24 * 7, sameSite: 'lax', path: '/',
  })
  if (token && to.query.preview === token) {
    bypass.value = '1'
    return
  }
  if (bypass.value === '1') return

  const isDa = to.path === '/da' || to.path.startsWith('/da/')
  return navigateTo(isDa ? '/da/coming-soon' : '/coming-soon')
})
