import type { Metadata, Viewport } from "next";
import Script from "next/script";
import { SerwistProvider } from "@serwist/turbopack/react";
import { SessionProvider } from "@/providers/session-provider";
import { BehaviorAnalyticsScripts } from "@/providers/behavior-analytics-scripts";
import { getBehaviorAnalyticsEndpoint } from "@/lib/analytics/behavior_analytics_config";
import { geistSans, geistMono, athelas } from "@/core/fonts";
import "./globals.css";

import { RootProviders } from "@/providers/root-providers";

export const metadata: Metadata = {
  title: "cozsweet",
  description: "cozsweet frontend (Next.js)",
  // Static web app manifest served from `public/`.
  manifest: "/manifest.json",
  // iOS Safari "添加到主屏" 元数据
  appleWebApp: {
    capable: true,
    statusBarStyle: "default",
    title: "cozsweet",
  },
  // iOS 触屏图标
  icons: {
    icon: "/favicon.ico",
    apple: "/favicon.ico",
  },
};

export const viewport: Viewport = {
  // 浏览器顶栏 / Android PWA 顶栏颜色
  themeColor: "#f84d96",
  // 让页面延伸到 notch / home indicator 区域，
  // env(safe-area-inset-bottom) 才能取到非零值（见 chat-input-bar.module.css）。
  viewportFit: "cover",
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html
      lang="en"
      className={`${geistSans.variable} ${geistMono.variable} ${athelas.variable} h-full antialiased`}
    >
      <body className="min-h-full flex flex-col">
        <Script id="cozsweet-analytics-config" strategy="beforeInteractive">
          {`window.COZSWEET_ANALYTICS_ENDPOINT=${JSON.stringify(
            getBehaviorAnalyticsEndpoint(),
          )}`}
        </Script>
        <BehaviorAnalyticsScripts />
        {/* NextAuth SessionProvider：为所有客户端组件提供 useSession() 上下文 */}
        <SessionProvider>
          <RootProviders>
            {/* SerwistProvider registers the service worker built from app/sw.ts. */}
            <SerwistProvider swUrl="/serwist/sw.js">{children}</SerwistProvider>
          </RootProviders>
        </SessionProvider>
      </body>
    </html>
  );
}
