import type { Metadata } from "next";

import { signOut } from "@/auth";
import { requireKitaSession } from "@/lib/auth-utils";
import { prisma } from "@/lib/prisma";
import { DashboardShell } from "@/components/dashboard/dashboard-shell";

export const metadata: Metadata = {
  robots: {
    index: false,
    follow: false,
  },
};

// =====================================================================
// Dashboard-Layout · Responsive App Shell
// ---------------------------------------------------------------------
// Server Component: liest kitaId aus der Session, holt den Kita-Namen
// aus der DB und rendert die responsive DashboardShell (mit Desktop-Sidebar
// und Mobile-Drawer).
// =====================================================================

export default async function DashboardLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const session = await requireKitaSession();

  const kita = await prisma.kita.findUniqueOrThrow({
    where: { id: session.user.kitaId },
    select: { name: true },
  });
  const displayName = session.user.name ?? session.user.email ?? "Kita-Planer";

  async function handleSignOut() {
    "use server";
    await signOut({ redirectTo: "/" });
  }

  return (
    <DashboardShell
      kitaName={kita.name}
      userName={displayName}
      userRole={session.user.role}
      signOutAction={handleSignOut}
    >
      {children}
    </DashboardShell>
  );
}

