import "@/app/global.css";
import type { Metadata } from "next";
import { ReactNode } from "react";
import Header from "@/components/Header/Header";
import Footer from "@/components/Footer/Footer";
import Container from "@/components/Container/Container";
import CookieConsentWrapper from "@/contexts/CookieConsentWrapper";
import CookieConsent from "@/components/CookieConsent/CookieConsent";
import ScriptManager from "@/components/ScriptManager/ScriptManager";
import AppContextWrapper from "@/contexts/AppContextWrapper";
import { lexend } from "@/app/fonts";
import DeferredGlobalClientEffects from "@/components/Deferred/DeferredGlobalClientEffects";

import { DEFAULT_OG_IMAGE, SITE_NAME } from "@/utils/constants";

const title = `Web Design, Branding & Marketing in Somerset | ${SITE_NAME}`;
const description = "We're a full-service brand growth agency offering branding, web design and digital marketing expertise including SEO, PPC, email and social media marketing.";
const image = DEFAULT_OG_IMAGE;

const themeBootstrapScript = `
(() => {
  const cookieMatch = document.cookie.match(/(?:^|;\\s*)theme=(dark|light)/);
  const theme = cookieMatch?.[1] || (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
  document.documentElement.setAttribute("data-theme", theme);
})();
`;

export const metadata: Metadata = {
  metadataBase: new URL(process.env.SITE_URL),
  title: title,
  description: description,
  keywords: [
    "Web",
    "Design",
    "Digital",
    SITE_NAME,
    "Branding",
    "SEO",
    "Social Media Marketing",
    "PPC",
    "Email Marketing",
  ],
  openGraph: {
    title: title,
    description: description,
    url: process.env.SITE_URL,
    siteName: SITE_NAME,
    images: [
      {
        url: image,
        width: 1200,
        height: 630,
      },
    ],
    locale: "en_GB",
    type: "website",
  },
  twitter: {
    card: "summary_large_image",
    title: title,
    description: description,
    images: [image],
  },
};

const RootLayout = ({ children }: { children: ReactNode }) => {
  return (
      <html
        lang="en"
        data-theme="light"
        className={lexend.variable}
        suppressHydrationWarning
      >
        <head>
          <script dangerouslySetInnerHTML={{ __html: themeBootstrapScript }} />
        </head>
        <body>
          <CookieConsentWrapper>
            <AppContextWrapper site="teapot">
              <DeferredGlobalClientEffects />
              <Header />
              <Container>
                <div className="strap" />
              </Container>
              <main id="main-content">{children}</main>
              <Footer />
              <CookieConsent />
              <ScriptManager />
            </AppContextWrapper>
          </CookieConsentWrapper>
        </body>
      </html>
  );
};

export default RootLayout;
