import { ReactNode } from "react";
import { redirect } from "next/navigation";
import { DashboardShell } from "@/components/layout/dashboard-shell";
import { requireOrganizationUser } from "@/src/modules/auth/auth.guard";

export default async function AppLayout({ children }: { children: ReactNode }) {
  try {
    const user = await requireOrganizationUser();
    if (user.isSuperAdmin) {
      redirect("/admin");
    }

    return <DashboardShell user={user}>{children}</DashboardShell>;
  } catch (error: any) {
    if (error?.message === "UNAUTHORIZED") {
      redirect("/login");
    }
    redirect("/login");
  }
}
