import type { Metadata, Viewport } from "next";
import Script from "next/script";
import { ClientProviders } from "@/components/providers/ClientProviders";
import { OrganizationJsonLd, WebSiteJsonLd } from "@/components/public/JsonLd";
import { fetchPublicSettings, socialUrlsFromSettings } from "@/lib/seo";
import "./globals.css";

const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || "https://belgianbitcoinembassy.org";

const plausibleDomain = process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN?.trim();
const plausibleAnalyticsOrigin = process.env.NEXT_PUBLIC_PLAUSIBLE_ANALYTICS_ORIGIN?.trim().replace(
  /\/$/,
  "",
);
const plausibleScriptSrc =
  plausibleDomain && plausibleAnalyticsOrigin
    ? `${plausibleAnalyticsOrigin}/js/script.js`
    : null;

export const metadata: Metadata = {
  metadataBase: new URL(siteUrl),
  title: {
    default: "Belgian Bitcoin Embassy | Bitcoin Meetups & Education in Belgium",
    template: "%s | Belgian Bitcoin Embassy",
  },
  description:
    "Discover Bitcoin meetups across Belgium. Real conversations, education, and a strong local community.",
  authors: [{ name: "Belgian Bitcoin Embassy" }],
  creator: "Belgian Bitcoin Embassy",
  publisher: "Belgian Bitcoin Embassy",
  openGraph: {
    type: "website",
    locale: "en_BE",
    siteName: "Belgian Bitcoin Embassy",
    title: "Belgian Bitcoin Embassy | Bitcoin Meetups & Education in Belgium",
    description:
      "Discover Bitcoin meetups across Belgium. Real conversations, education, and a strong local community.",
    images: [
      {
        url: "/og-default.png",
        width: 1200,
        height: 630,
        alt: "Belgian Bitcoin Embassy - Bitcoin Meetups & Education in Belgium",
      },
    ],
  },
  twitter: {
    card: "summary_large_image",
    title: "Belgian Bitcoin Embassy",
    description:
      "Discover Bitcoin meetups across Belgium. Real conversations, education, and a strong local community.",
    images: ["/og-default.png"],
  },
  robots: {
    index: true,
    follow: true,
    googleBot: {
      index: true,
      follow: true,
      "max-video-preview": -1,
      "max-image-preview": "large",
      "max-snippet": -1,
    },
  },
  icons: {
    icon: [
      { url: "/favicon.svg", type: "image/svg+xml" },
      { url: "/favicon.ico", sizes: "32x32" },
    ],
    apple: "/apple-touch-icon.png",
  },
  alternates: {
    canonical: "/",
  },
};

export const viewport: Viewport = {
  themeColor: "#F7931A",
  width: "device-width",
  initialScale: 1,
};

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const settings = await fetchPublicSettings();
  const sameAs = socialUrlsFromSettings(settings);
  return (
    <html lang="en" dir="ltr" className="dark">
      <body>
        {plausibleScriptSrc && plausibleDomain ? (
          <Script
            defer
            src={plausibleScriptSrc}
            data-domain={plausibleDomain}
            strategy="afterInteractive"
          />
        ) : null}
        <OrganizationJsonLd sameAs={sameAs} />
        <WebSiteJsonLd />
        <ClientProviders>{children}</ClientProviders>
      </body>
    </html>
  );
}
