import { createClient } from "@/lib/supabase/server";
import { redirect } from "next/navigation";
import { Suspense } from "react";
import { OnboardingLinkedInStep } from "@/components/onboarding-linkedin-step";

async function LinkedInOnboardingGuard() {
  const supabase = await createClient();
  const { data } = await supabase.auth.getUser();

  if (!data?.user) {
    redirect("/auth/login");
  }

  // Must have completed org step first
  const { data: profile } = await supabase
    .from("profiles")
    .select("organization_id")
    .eq("id", data.user.id)
    .single();

  if (!profile?.organization_id) {
    redirect("/onboarding");
  }

  return <OnboardingLinkedInStep />;
}

export default function LinkedInOnboardingPage() {
  return (
    <Suspense fallback={<div className="flex h-screen items-center justify-center">Loading…</div>}>
      <LinkedInOnboardingGuard />
    </Suspense>
  );
}
