import { getAulaContext } from "@/lib/data/aula-context";
import Loading from "@/components/ui/Loading";
import DashboardShell from "@/components/dashboard/DashboardShell";
import { requireUser } from "@/lib/auth/session";
import { Suspense, type ReactNode } from "react";

// Entry to the private dashboard may redirect before its children can render.
// Validate instant navigation within the dashboard, after this auth boundary.
export const instant = false;

export default function DashboardLayout({ children }: { children: ReactNode }) {
  return (
    <Suspense fallback={<Loading fullScreen />}>
      <AuthenticatedDashboard>{children}</AuthenticatedDashboard>
    </Suspense>
  );
}

async function AuthenticatedDashboard({ children }: { children: ReactNode }) {
  const user = await requireUser();
  const { aula, aulas } = await getAulaContext();
  return (
    <DashboardShell
      key={user.id}
      aula={
        aula
          ? {
              id: aula.aul_id,
              nombre: aula.aul_curso_nombre || "Curso",
              clave: aula.aul_clave_registro || "",
            }
          : undefined
      }
      aulas={aulas.map((a) => ({
        id: a.aul_id,
        nombre: a.aul_curso_nombre || "Curso",
        clave: a.aul_clave_registro || "",
      }))}
      user={user}
    >
      {children}
    </DashboardShell>
  );
}
