import type { ReactNode } from 'react'
import { Navigate, useLocation } from 'react-router-dom'
import { onboardingRedirectPath } from '../lib/onboarding'
import { useOnboarding } from '../onboarding/OnboardingContext'

export function OnboardingRouteGuard({ children }: { children: ReactNode }) {
  const { pathname } = useLocation()
  const { loading, isComplete } = useOnboarding()

  const redirectTo = !loading ? onboardingRedirectPath(pathname, isComplete) : null
  if (redirectTo) return <Navigate to={redirectTo} replace />

  return children
}