import { router } from "expo-router";
import { useEffect, useRef } from "react";

import { api } from "@/lib/trpc";

/** Redirect new users into onboarding until they complete setup. */
export function OnboardingGate() {
  const statusQuery = api.settings.getOnboardingStatus.useQuery();
  const redirected = useRef(false);

  useEffect(() => {
    if (redirected.current || statusQuery.isLoading || !statusQuery.data) return;
    if (statusQuery.data.completed) return;
    redirected.current = true;
    router.push("/(app)/onboarding" as never);
  }, [statusQuery.data, statusQuery.isLoading]);

  return null;
}
