import type { Metadata } from "next";
import { Baby, LogOut } from "lucide-react";

import { signOut } from "@/auth";
import { requireKitaSession } from "@/lib/auth-utils";
import { prisma } from "@/lib/prisma";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { SidebarNav } from "@/components/dashboard/sidebar-nav";

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

// =====================================================================
// Dashboard-Layout · Linke Sidebar
// ---------------------------------------------------------------------
// Server Component: liest kitaId aus der Session, holt den Kita-Namen
// aus der DB und rendert das App-Shell (Sidebar + main content area).
// Der eigentliche Seiteninhalt kommt via {children} rein.
// =====================================================================

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 },
  });

  return (
    <div className="flex min-h-screen bg-muted/30">
      {/* ── Sidebar ───────────────────────────────────────────────────── */}
      <aside className="flex w-60 shrink-0 flex-col border-r bg-background">
        {/* Logo / Kita-Name */}
        <div className="flex h-16 items-center gap-2.5 border-b px-5">
          <div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-md bg-primary text-primary-foreground">
            <Baby className="h-4 w-4" />
          </div>
          <div className="min-w-0">
            <p className="truncate text-sm font-semibold leading-none">
              {kita.name}
            </p>
            <p className="mt-0.5 text-xs text-muted-foreground">Kita-Planer</p>
          </div>
        </div>

        {/* Navigation */}
        <div className="flex-1 overflow-y-auto py-4">
          <SidebarNav role={session.user.role} />
        </div>

        {/* Footer: User-Info + Abmelden */}
        <div className="border-t p-3">
          <Separator className="mb-3" />
          <div className="mb-2 px-3">
            <p className="truncate text-xs font-medium">
              {session.user.name ?? session.user.email}
            </p>
            <p className="truncate text-xs text-muted-foreground">
              {session.user.role}
            </p>
          </div>
          <form
            action={async () => {
              "use server";
              await signOut({ redirectTo: "/" });
            }}
          >
            <Button
              type="submit"
              variant="ghost"
              size="sm"
              className="w-full justify-start gap-2 text-muted-foreground"
            >
              <LogOut className="h-4 w-4" />
              Abmelden
            </Button>
          </form>
        </div>
      </aside>

      {/* ── Main Content ──────────────────────────────────────────────── */}
      <main className="flex-1 overflow-y-auto">
        {children}
      </main>
    </div>
  );
}
