import type { Metadata } from "next";
import { LogOut, ShieldCheck } from "lucide-react";
import { UserRole } from "@prisma/client";

import { signOut } from "@/auth";
import { requireRole } from "@/lib/auth-utils";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { AdminNav } from "./_components/admin-nav";

export const metadata: Metadata = {
  title: "Systemadmin · Kita-Planer",
  robots: {
    index: false,
    follow: false,
  },
};

export default async function AdminLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const session = await requireRole([UserRole.SUPERADMIN]);

  return (
    <div className="flex min-h-screen bg-muted/30">
      <aside className="flex w-64 shrink-0 flex-col border-r bg-background">
        <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">
            <ShieldCheck className="h-4 w-4" />
          </div>
          <div className="min-w-0">
            <p className="truncate text-sm font-semibold leading-none">
              Systemadmin
            </p>
            <p className="mt-0.5 text-xs text-muted-foreground">
              Kita-Planer
            </p>
          </div>
        </div>

        <div className="flex-1 overflow-y-auto py-4">
          <AdminNav />
        </div>

        <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">
              SUPERADMIN
            </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 className="flex-1 overflow-y-auto">{children}</main>
    </div>
  );
}

