import "../styles/globals.css";
import "../styles/builder.css";
import type { ReactNode } from "react";
import { headers, cookies } from "next/headers";
import { LocaleProvider } from "@/features/i18n/LocaleProvider";
import { LocaleSwitcher } from "@/features/i18n/LocaleSwitcher";
import { resolveLocaleFromAcceptLanguage, DEFAULT_LOCALE } from "@/features/i18n/locale";

export const metadata = {
  title: "Page Builder",
  description: "No-code single page builder",
};

export default async function RootLayout({ children }: { children: ReactNode }) {
  const cookieStore = await cookies();
  const cookieLocale = cookieStore.get("pb-locale")?.value;

  const headerList = await headers();
  const acceptLanguage = headerList.get("accept-language");

  const localeFromCookie = cookieLocale === "en" || cookieLocale === "ko" ? cookieLocale : null;
  const locale = (localeFromCookie ?? resolveLocaleFromAcceptLanguage(acceptLanguage ?? undefined)) ?? DEFAULT_LOCALE;

  return (
    <html lang={locale} suppressHydrationWarning>
      <body className="min-h-screen bg-slate-50 text-slate-900 dark:bg-slate-950 dark:text-slate-50">
        <LocaleProvider initialLocale={locale}>
				{children}
				<LocaleSwitcher />
			</LocaleProvider>
      </body>
    </html>
  );
}
