import type { Metadata } from "next";
import { cookies } from "next/headers";
import "@/scss/nbn.scss";
import NavbarClient from "@/components/Navbar";

export const metadata: Metadata = {
    title: "Spectrum Next Explorer",
    description: "A platform for exploring the Spectrum Next ecosystem",
    robots: { index: true, follow: true },
    formatDetection: { email: false, address: false, telephone: false },
};

function themeInitScript() {
    return `(function(){
      try {
        var cookie = document.cookie.split('; ').find(function(c){return c.indexOf('NBN-theme=')===0});
        var value = cookie ? decodeURIComponent(cookie.split('=').slice(1).join('=')) : null;
        var theme;
        if (value === 'light' || value === 'dark') {
          theme = value;
        } else {
          var prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
          theme = prefersDark ? 'dark' : 'light';
        }
        var html = document.documentElement;
        if (html.getAttribute('data-bs-theme') !== theme) {
          html.setAttribute('data-bs-theme', theme);
        }
      } catch(_) {}
    })();`;
}

export default async function RootLayout({ children }: { children: React.ReactNode }) {
    const cookieStore = await cookies();
    const stored = cookieStore.get("NBN-theme")?.value;
    const serverTheme = stored === "light" || stored === "dark" ? stored : "light";

    return (
        <html lang="en" suppressHydrationWarning data-bs-theme={serverTheme}>
        <head>
            <script dangerouslySetInnerHTML={{ __html: themeInitScript() }} />
        </head>
        <body>
        <NavbarClient />
        <div className="container-fluid py-3">{children}</div>
        </body>
        </html>
    );
}