import type { Metadata, Viewport } from "next";
import type { ReactNode } from "react";

import { Poppins, DM_Sans } from "next/font/google";
import NextTopLoader from "nextjs-toploader";
import "./globals.css";

import ModalRenderer from "../lib/modal-system/ModalRender";
import Providers from "../store/providers";
import ScrollToTop from "../components/ScrollToTop";
import WelcomeModal from "../components/modals/ComingSoon";
import ComingSoonWrapper from "../components/ComingSoonWrapper";
import { getOrganizationSchema } from "../lib/schema/organization";

export const viewport: Viewport = {
  width: "device-width",
  initialScale: 1,
};
const poppins = Poppins({
  subsets: ["latin"],
  weight: ["300", "400", "500", "600", "700", "800"],
  variable: "--font-poppins",
});

const dmSans = DM_Sans({
  subsets: ["latin"],
  weight: ["400", "500", "600", "700"],
  variable: "--font-dm-sans",
});

export function generateMetadata(): Metadata {
  const baseUrl = process.env.NEXT_PUBLIC_APP_URL || "https://www.kleboai.com";

  return {
    title: {
      default: "KleboAI – AI Image Detector",
      template: "%s | KleboAI",
    },
    description:
      "AI-powered image analysis to detect AI-generated content. Analyze any image instantly with advanced detection models.",
    metadataBase: new URL(baseUrl),
    manifest: "/manifest.json",
    openGraph: {
      title: "KleboAI – AI Image Detector",
      description:
        "Analyze any image instantly to detect AI-generated content. Get a confidence score powered by advanced detection models.",
      url: baseUrl,
      siteName: "KleboAI",
      images: [
        {
          url: `${baseUrl}/og-image.png`,
          width: 1200,
          height: 630,
          alt: "KleboAI AI Image Detection",
        },
      ],
      locale: "en_US",
      type: "website",
    },
    twitter: {
      card: "summary_large_image",
      title: "KleboAI – AI Image Detector ",
      description:
        "Analyze any image instantly to detect AI-generated content. Get a confidence score powered by advanced detection models.",
      images: [`${baseUrl}/og-image.png`],
    },
  };
}

interface RootLayoutProps {
  children: ReactNode;
}

export default function RootLayout({ children }: RootLayoutProps) {
  const baseUrl = process.env.NEXT_PUBLIC_APP_URL || "https://www.kleboai.com";
  const organizationSchema = getOrganizationSchema(baseUrl);

  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{
            __html: JSON.stringify(organizationSchema),
          }}
        />
      </head>
      <body className={`bg-body-bg ${poppins.variable} ${dmSans.variable}`}>
        <NextTopLoader
          color="#3b82f6"
          initialPosition={0.08}
          crawlSpeed={50}
          height={3}
          crawl={true}
          easing="ease"
          speed={50}
          shadow="0 0 10px #3b82f6,0 0 5px #3b82f6"
          showSpinner={false}
        />
        <Providers>
          <ComingSoonWrapper />
          {children}
          <ScrollToTop />
          <ModalRenderer />
        </Providers>
      </body>
    </html>
  );
}
