import type { Metadata } from "next";
import { AntdRegistry } from "@ant-design/nextjs-registry";
import { AuthProvider } from "@/lib/auth/AuthProvider";
import { BrandingProvider } from "@/lib/branding/BrandingProvider";
import { ThemeProvider } from "@/lib/theme/ThemeProvider";
import { getPublicSettings } from "@/lib/api/settings";
import "./globals.css";

export async function generateMetadata(): Promise<Metadata> {
  const settings = await getPublicSettings();

  return {
    title: settings.site_title || "Domain/Ops",
    robots: settings.noindex_nofollow ? { index: false, follow: false } : undefined,
    // Without an uploaded favicon the tab shows the same app-grid mark as the
    // logo fallback in the header and on the login card.
    icons: { icon: settings.site_favicon || "/appstore-icon.svg" },
  };
}

export default async function RootLayout({ children }: LayoutProps<"/">) {
  const settings = await getPublicSettings();

  return (
    <html lang={settings.default_language} className="h-full">
      <body className="min-h-full flex flex-col antialiased app-root">
        {settings.custom_css && <style id="dms-custom-css">{settings.custom_css}</style>}
        <AntdRegistry>
          <ThemeProvider theme={settings.theme}>
            <BrandingProvider branding={{ site_title: settings.site_title, site_logo: settings.site_logo, demo_mode: settings.demo_mode }}>
              <AuthProvider>{children}</AuthProvider>
            </BrandingProvider>
          </ThemeProvider>
        </AntdRegistry>
      </body>
    </html>
  );
}
