export default defineNuxtRouteMiddleware(async (to) => {
  const auth = useAuthStore()
  const isLogin = to.path === '/login'

  if (!auth.checked) {
    await auth.ensureSession()
  }

  if (!isLogin && !auth.isAuthenticated) {
    return navigateTo('/login')
  }

  if (isLogin && auth.isAuthenticated) {
    return navigateTo('/')
  }
})
