import { redirect } from "next/navigation";
import { createClient } from "@/lib/supabase/server";
import DashboardLayoutClient from "@/components/DashboardLayoutClient";

export default async function DashboardLayout({ children }: { children: React.ReactNode }) {
  const supabase = await createClient();
  const {
    data: { user },
  } = await supabase.auth.getUser();

  if (!user) redirect("/login");

  const { data: profile } = await supabase
    .from("profiles")
    .select("full_name, email, role, avatar_url")
    .eq("id", user.id)
    .single();

  if (!profile) {
    // If auth user exists but profile does not, send to login to re-establish
    redirect("/login");
  }

  return (
    <DashboardLayoutClient profile={profile as any}>
      {children}
    </DashboardLayoutClient>
  );
}
