import { redirect } from "next/navigation";
import { OnboardingWizard } from "@/components/onboarding/OnboardingWizard";
import { API_BASE_URL } from "@/lib/config";

export const dynamic = "force-dynamic";

interface OnboardingStatusResponse {
  completed: boolean;
}

async function getOnboardingStatus(): Promise<OnboardingStatusResponse> {
  try {
    const response = await fetch(`${API_BASE_URL}/api/onboarding/status`, {
      method: "GET",
      cache: "no-store",
    });

    if (!response.ok) {
      return { completed: false };
    }

    return (await response.json()) as OnboardingStatusResponse;
  } catch {
    return { completed: false };
  }
}

export default async function OnboardingPage(): Promise<React.JSX.Element> {
  const status = await getOnboardingStatus();

  if (status.completed) {
    redirect("/");
  }

  return <OnboardingWizard />;
}
