import type { Metadata, Viewport } from "next";
import { Figtree, JetBrains_Mono } from "next/font/google";
import { ThemeProvider } from "@/components/ThemeProvider";
import Navbar from "@/components/Navbar";
import TabWatcher from "@/components/TabWatcher";
import Footer from "@/components/Footer";
import ScrollToTop from "@/components/ScrollToTop";
import CookieBanner from "@/components/CookieBanner";
import { cn } from "@/lib/utils";
import { siteConfig } from "@/lib/config/site";
import { buildSignature, buildSignatureCompact } from "@/lib/config/build-signature";
import "./globals.css";

const figtree = Figtree({
  variable: "--font-figtree",
  subsets: ["latin"],
  weight: ["300", "400", "500", "600", "700", "800"],
});

const jetBrainsMono = JetBrains_Mono({
  variable: "--font-jetbrains-mono",
  subsets: ["latin"],
  weight: ["400", "500", "600", "700"],
});

const { seo, icons, social, url, fullName, name } = siteConfig;

export const metadata: Metadata = {
  metadataBase: new URL(url),
  title: {
    default: seo.title,
    template: seo.titleTemplate,
  },
  description: seo.description,
  keywords: [...seo.keywords],
  authors: [{ name: `${name} Team`, url }],
  creator: `${name} Team`,
  publisher: fullName,
  category: "technology",
  robots: {
    index: true,
    follow: true,
    googleBot: {
      index: true,
      follow: true,
      "max-image-preview": "large",
      "max-snippet": -1,
    },
  },
  alternates: {
    canonical: url,
  },
  manifest: icons.manifest,
  openGraph: {
    type: "website",
    locale: "id_ID",
    alternateLocale: ["en_US", "en_GB"],
    url,
    title: seo.ogTitle,
    description: seo.ogDescription,
    siteName: fullName,
    images: [
      {
        url: seo.ogImage,
        width: 1200,
        height: 630,
        alt: seo.ogImageAlt,
        type: "image/png",
      },
    ],
  },
  twitter: {
    card: "summary_large_image",
    title: seo.twitterTitle,
    description: seo.twitterDescription,
    images: [seo.ogImage],
    creator: social.twitter,
    site: social.twitter,
  },
  icons: {
    icon: [
      { url: icons.favicon },
      { url: icons.faviconSvg, type: "image/svg+xml" },
      { url: icons.faviconPng, type: "image/png" },
    ],
    apple: icons.apple,
    shortcut: icons.favicon,
  },
};

export const viewport: Viewport = {
  themeColor: seo.themeColor,
  width: "device-width",
  initialScale: 1,
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html
      lang="id"
      className="scroll-smooth"
      data-v={buildSignature.a}
      data-k={buildSignature.d}
      suppressHydrationWarning
    >
      <head>
        <script
          dangerouslySetInnerHTML={{
            __html: `(function(){try{var t=localStorage.getItem('theme');var c=(t==='light'||t==='dark')?t:(window.matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light');document.documentElement.classList.add(c);}catch(e){}})();`,
          }}
        />
        <link rel="preconnect" href="https://discord.com" />
        <link rel="preconnect" href="https://api.dicebear.com" />
      </head>
      <body
        data-i={buildSignatureCompact}
        className={cn(
          figtree.variable,
          jetBrainsMono.variable,
          "font-sans bg-base text-content antialiased min-h-screen flex flex-col selection:bg-accent-hover/30 selection:text-accent-text"
        )}
      >
        <ThemeProvider>
          <Navbar />
          <TabWatcher />
          <main className="grow relative overflow-x-hidden">{children}</main>
          <Footer />
          <ScrollToTop />
          <CookieBanner />
        </ThemeProvider>
      </body>
    </html>
  );
}
