import type { Metadata, Viewport } from "next"
import { Space_Grotesk } from "next/font/google"
import "../globals.css"
import "@ui/globals.css"
import { AuthProvider } from "@lib/auth-context"
import { ErrorTrackingProvider } from "@lib/error-tracking"
import { PostHogProvider } from "@lib/posthog"
import { QueryProvider } from "../components/query-client"
import { AutumnProvider } from "autumn-js/react"
import { Suspense } from "react"
import { Toaster } from "@ui/components/sonner"
import { NuqsAdapter } from "nuqs/adapters/next/app"
import { ThemeProvider } from "@/lib/theme-provider"

const font = Space_Grotesk({
	subsets: ["latin"],
	variable: "--font-sans",
})

export const metadata: Metadata = {
	metadataBase: new URL("https://app.supermemory.ai"),
	description: "Your memories, wherever you are",
	icons: {
		icon: [
			{ url: "/favicon.ico", sizes: "any" },
			{ url: "/favicon-16x16.png", sizes: "16x16", type: "image/png" },
			{ url: "/favicon-32x32.png", sizes: "32x32", type: "image/png" },
		],
		apple: [
			{ url: "/apple-touch-icon.png", sizes: "180x180", type: "image/png" },
		],
	},
	manifest: "/manifest.webmanifest",
	openGraph: {
		description: "Your memories, wherever you are",
		images: [
			{
				url: "/OG.png",
				width: 1800,
				height: 945,
				alt: "supermemory app",
			},
		],
		siteName: "supermemory",
		title: "supermemory app",
		type: "website",
	},
	title: "supermemory app",
	twitter: {
		card: "summary_large_image",
		description: "Your memories, wherever you are",
		images: ["/OG.png"],
		title: "supermemory app",
	},
}

export const viewport: Viewport = {
	width: "device-width",
	initialScale: 1,
	viewportFit: "cover",
	interactiveWidget: "resizes-content",
}

export default function RootLayout({
	children,
}: Readonly<{
	children: React.ReactNode
}>) {
	return (
		<html lang="en" suppressHydrationWarning>
			<head>
				{process.env.NODE_ENV === "development" && (
					<script
						crossOrigin="anonymous"
						src="https://unpkg.com/react-scan/dist/auto.global.js"
					/>
				)}
			</head>
			<body
				className={`${font.variable} antialiased overflow-x-hidden`}
				suppressHydrationWarning
			>
				<ThemeProvider
					attribute="class"
					defaultTheme="dark"
					enableSystem={false}
					disableTransitionOnChange
					forcedTheme="dark"
				>
					<AutumnProvider
						backendUrl={
							process.env.NEXT_PUBLIC_BACKEND_URL ??
							"https://api.supermemory.ai"
						}
						includeCredentials={true}
						headers={{ "X-App-Source": "nova" }}
					>
						<QueryProvider>
							<AuthProvider>
								<PostHogProvider>
									<ErrorTrackingProvider>
										<NuqsAdapter>
											<Suspense>{children}</Suspense>
											<Toaster />
										</NuqsAdapter>
									</ErrorTrackingProvider>
								</PostHogProvider>
							</AuthProvider>
						</QueryProvider>
					</AutumnProvider>
				</ThemeProvider>
			</body>
		</html>
	)
}
