import type { Metadata } from "next";
import "./globals.css";
import "./forgeworks-interface.css";

export const metadata: Metadata = {
  metadataBase: new URL("https://batchforge.myforgeworks.net"),
  title: "BatchForge — Local Image to WebP Converter",
  description: "Convert entire batches of PNG, JPG, HEIC, TIFF and more to WebP. Fast, private, and entirely on your device.",
  openGraph: {
    title: "BatchForge — Local Image to WebP Converter",
    description: "Convert entire image batches to WebP without uploading a single file.",
    images: [{ url: "/og.png", width: 1536, height: 1024, alt: "BatchForge local image converter" }],
  },
  twitter: {
    card: "summary_large_image",
    title: "BatchForge — Local Image to WebP Converter",
    description: "Convert entire image batches to WebP without uploading a single file.",
    images: ["/og.png"],
  },
  icons: {
    icon: [{ url: "/batchforge-icon-v3.svg", type: "image/svg+xml" }],
    shortcut: "/batchforge-icon-v3.svg",
  },
};

const themeBoot = `
  (() => {
    const key = "forgeworks-theme";
    const allowed = ["light", "gray", "black"];
    const hostname = window.location.hostname.toLowerCase();
    const sharesTheme = hostname === "myforgeworks.net" || hostname.endsWith(".myforgeworks.net");
    let cookieTheme = null;
    let storedTheme = null;

    try {
      if (sharesTheme) {
        const prefix = key + "=";
        const item = document.cookie.split("; ").find((part) => part.startsWith(prefix));
        cookieTheme = item ? decodeURIComponent(item.slice(prefix.length)) : null;
      }
    } catch (_) {}

    try {
      storedTheme = localStorage.getItem(key);
    } catch (_) {}

    const theme = allowed.includes(cookieTheme)
      ? cookieTheme
      : allowed.includes(storedTheme)
        ? storedTheme
        : "gray";

    document.documentElement.dataset.theme = theme;
    document.documentElement.style.colorScheme = theme === "light" ? "light" : "dark";

    try {
      localStorage.setItem(key, theme);
    } catch (_) {}

    if (sharesTheme) {
      document.cookie = key + "=" + encodeURIComponent(theme) + "; Max-Age=31536000; Path=/; Domain=myforgeworks.net; SameSite=Lax; Secure";
    }
  })();
`;

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en" data-theme="gray" suppressHydrationWarning>
      <head>
        <meta name="codex-preview" content="development" />
        <script dangerouslySetInnerHTML={{ __html: themeBoot }} />
      </head>
      <body>{children}</body>
    </html>
  );
}
