import type { Metadata, Viewport } from "next";
import { headers } from "next/headers";
import { Inter } from "next/font/google";
import "./globals.css";

const inter = Inter({
  variable: "--font-inter",
  subsets: ["latin"],
  display: "swap",
});

const PRODUCTION_BASE = "https://catalogo.abelharainha.com.br";

async function resolveBaseURL(): Promise<URL> {
  try {
    const h = await headers();
    // O host original via X-Forwarded-Host; fallback para o Host direto.
    const host = h.get("x-forwarded-host") || h.get("host");
    const proto = h.get("x-forwarded-proto") || (host?.startsWith("localhost") ? "http" : "https");
    if (host) return new URL(`${proto}://${host}`);
  } catch {}
  return new URL(PRODUCTION_BASE);
}

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

  return {
    metadataBase: baseURL,
    title: "Catálogo Digital Abelha Rainha | CP 03/2026",
    description: "Catálogo digital premium Abelha Rainha Campanha 03/2026. Explore lançamentos, promoções, kits e todos os produtos de beleza. 36 anos cuidando da sua beleza.",
    keywords: [
      "Abelha Rainha", "catálogo digital", "cosméticos", "beleza",
      "CP 03/2026", "perfumaria", "maquiagem", "cuidados pessoais",
      "lançamentos", "promoções", "kits promocionais",
    ],
    authors: [{ name: "Abelha Rainha" }],
    creator: "Abelha Rainha",
    publisher: "Abelha Rainha",
    manifest: "/manifest.json",
    icons: {
      icon: "/logo.png",
      apple: "/logo.png",
      shortcut: "/logo.png",
    },
    openGraph: {
      title: "Catálogo Digital Abelha Rainha | CP 03/2026",
      description: "Compre já pelo próprio catálogo virtual ",
      url: baseURL.toString(),
      siteName: "Catálogo Abelha Rainha",
      locale: "pt_BR",
      type: "website",
      images: [
        {
          // Capa do catálogo (página 1) — usada na pré-visualização do
          // WhatsApp / Telegram / Facebook quando alguém compartilha o link.
          // WebP 971x1400 (retrato) — o WhatsApp recorta/ajusta automaticamente,
          // e essa proporção preenche bem o card grande de preview.
          url: "/og-cover.webp",
          width: 971,
          height: 1400,
          alt: "Catálogo Abelha Rainha — CP 03/2026",
          type: "image/webp",
        },
      ],
    },
    twitter: {
      card: "summary_large_image",
      title: "Catálogo Digital Abelha Rainha CP 03/2026",
      description: "Compre já pelo próprio catálogo virtual ",
      images: ["/og-cover.webp"],
    },
    robots: {
      index: true,
      follow: true,
      googleBot: {
        index: true,
        follow: true,
        "max-image-preview": "large",
      },
    },
    alternates: {
      canonical: baseURL.toString(),
    },
    category: "shopping",
  };
}

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

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="pt-BR" suppressHydrationWarning>
      <head>
        <link rel="preconnect" href="https://fonts.googleapis.com" />
        <link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="anonymous" />
      </head>
      <body
        className={`${inter.variable} antialiased bg-white text-gray-900`}
      >
        {children}
      </body>
    </html>
  );
}
