import { getServerSession } from "next-auth";
import { redirect } from "next/navigation";
import { authOptions } from "@/lib/auth/options";
import { AdminLayoutClientShell } from "@/components/admin/admin-nav";
import { SETTING_KEYS } from "@/lib/constants";
import { getSetting } from "@/lib/settings";
import { AccentColorScript } from "@/components/layout/accent-color";

export default async function AdminLayout({ children }: { children: React.ReactNode }) {
  const session = await getServerSession(authOptions);
  if (!session?.user) redirect("/anmelden");
  if (session.user.role !== "ADMIN") redirect("/anmelden");

  let accentColor = "#4f46e5";
  try {
    const color = await getSetting(SETTING_KEYS.BRANDING_ACCENT_COLOR);
    if (color && /^#[0-9a-fA-F]{6}$/.test(color)) accentColor = color;
  } catch { /* default */ }

  return (
    <>
      <AccentColorScript color={accentColor} />
      <AdminLayoutClientShell>{children}</AdminLayoutClientShell>
    </>
  );
}
