import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import Web3ModalProvider from "@/context/Web3Modal";
import { MockWalletProvider } from "@/context/MockWalletContext";
import { headers } from "next/headers";
import { cookieToInitialState } from "wagmi";
import { config } from "@/config";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
  title: "MTE7 - Decentralized Portal",
  description: "Web3 MLM Platform on BSC",
};

export const viewport = {
  width: "device-width",
  initialScale: 1,
  maximumScale: 1,
};

export default async function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  const initialState = cookieToInitialState(config, (await headers()).get("cookie"));

  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <script
          dangerouslySetInnerHTML={{
            __html: `
              (function() {
                var suppressErrors = function(e) {
                  var msg = e && (e.message || (e.reason && e.reason.message) || '');
                  if (typeof msg === 'string' && (msg.indexOf('_0x') !== -1 || msg.indexOf('indexedDB') !== -1 || msg.indexOf('Clarity') !== -1)) {
                    if (e.preventDefault) e.preventDefault();
                    if (e.stopPropagation) e.stopPropagation();
                    console.warn('Suppressed third-party runtime error:', msg);
                    return true;
                  }
                };
                window.addEventListener('error', suppressErrors, true);
                window.addEventListener('unhandledrejection', suppressErrors, true);
              })();
            `
          }}
        />
      </head>
      <body className={`${inter.className} bg-slate-950 text-white min-h-screen`}>
        <Web3ModalProvider initialState={initialState}>
          <MockWalletProvider>
            {children}
          </MockWalletProvider>
        </Web3ModalProvider>
      </body>
    </html>
  );
}
