import { auth0 } from "@/lib/auth0"
import { redirect } from "next/navigation"
import { hasCompletedOnboarding } from "@codeflash-ai/common"
import { APP_ROUTES } from "@/lib/types"
import { Suspense } from "react"

async function HomeContent() {
  const session = await auth0.getSession()
  if (!session) return null

  const completedOnboarding = await hasCompletedOnboarding(session.user.sub)
  if (!completedOnboarding) {
    redirect("/onboarding")
  }

  redirect(APP_ROUTES.API_KEYS)
}

export default function RootPage() {
  return (
    <Suspense
      fallback={
        <div className="flex min-h-screen items-center justify-center">
          <div className="h-8 w-8 animate-spin rounded-full border-4 border-gray-300 border-t-blue-500" />
        </div>
      }
    >
      <HomeContent />
    </Suspense>
  )
}
