import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { localizedDomains } from "@/lib/domains";
import { getDictionary } from "@/lib/i18n";
import { getLocale } from "@/lib/locale";
import { getCurrentUser } from "@/lib/session";
import { PanelShell } from "./panel-shell";

export default async function PanelLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const locale = await getLocale();
  const user = await getCurrentUser(locale);
  if (!user || user.accountType !== "PANEL") redirect("/login");
  const dictionary = getDictionary(locale);
  const themeCookie = (await cookies()).get("capila_theme")?.value;
  return (
    <PanelShell
      user={user}
      locale={locale}
      dictionary={dictionary}
      domainLinks={localizedDomains(locale)}
      theme={themeCookie === "dark" ? "dark" : "light"}
    >
      {children}
    </PanelShell>
  );
}
