import type { Metadata } from "next";
import { Inter } from "next/font/google";
import dynamic from "next/dynamic";
import "./globals.css";
import SiteHeader from "@/components/layout/SiteHeader";
import SiteFooter from "@/components/layout/SiteFooter";

// Heavy client components — load lazily to keep the critical bundle small
const CompareFloatingBar = dynamic(
  () => import("@/components/compare/CompareFloatingBar"),
  { ssr: false }
);
const AIChatAssistant = dynamic(
  () => import("@/components/AIChatAssistant"),
  { ssr: false }
);
const BackToTop = dynamic(
  () => import("@/components/ui/BackToTop"),
  { ssr: false }
);

const inter = Inter({
  subsets: ["latin"],
  display: "swap",
  variable: "--font-inter",
  // Only load the weights we actually use — reduces font payload
  weight: ["400", "500", "600", "700", "800", "900"],
  preload: true,
});

const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL ?? "https://drivehub.in";

export const metadata: Metadata = {
  metadataBase: new URL(SITE_URL),
  title: {
    default: "DriveHub — New Cars, Prices & Reviews in India",
    template: "%s | DriveHub",
  },
  description:
    "Find new cars in India. Compare car prices, specs, reviews, images across all brands. Get EMI calculator, dealer offers and more.",
  keywords: [
    "new cars india", "car prices", "car comparison", "car reviews",
    "buy car india", "car specs", "ex showroom price", "car emi calculator",
    "suv india", "electric cars india",
  ],
  authors: [{ name: "DriveHub" }],
  creator: "DriveHub",
  publisher: "DriveHub",
  openGraph: {
    type: "website",
    locale: "en_IN",
    siteName: "DriveHub",
    url: SITE_URL,
    images: [
      {
        url: `${SITE_URL}/og-default.jpg`,
        width: 1200,
        height: 630,
        alt: "DriveHub — New Cars in India",
      },
    ],
  },
  twitter: {
    card: "summary_large_image",
    site: "@DriveHubIndia",
    creator: "@DriveHubIndia",
  },
  robots: {
    index: true,
    follow: true,
    googleBot: {
      index: true,
      follow: true,
      "max-image-preview": "large",
      "max-snippet": -1,
    },
  },
  alternates: {
    canonical: SITE_URL,
  },
  verification: {
    // Add Google Search Console verification token here when available
    // google: "your-verification-token",
  },
};

/** Organisation schema — injected once into every page via layout */
const organizationSchema = {
  "@context": "https://schema.org",
  "@type": "Organization",
  name: "DriveHub",
  url: SITE_URL,
  logo: `${SITE_URL}/logo.png`,
  description: "India's smartest automotive marketplace — new car prices, specs, reviews & comparisons.",
  sameAs: [
    "https://twitter.com/DriveHubIndia",
    "https://www.facebook.com/DriveHubIndia",
    "https://www.instagram.com/DriveHubIndia",
  ],
  contactPoint: {
    "@type": "ContactPoint",
    contactType: "customer support",
    availableLanguage: "English",
  },
};

/** Website search action schema */
const websiteSchema = {
  "@context": "https://schema.org",
  "@type": "WebSite",
  name: "DriveHub",
  url: SITE_URL,
  potentialAction: {
    "@type": "SearchAction",
    target: {
      "@type": "EntryPoint",
      urlTemplate: `${SITE_URL}/cars?search={search_term_string}`,
    },
    "query-input": "required name=search_term_string",
  },
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" className={inter.variable}>
      <head>
        {/* Structured data */}
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(organizationSchema) }}
        />
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(websiteSchema) }}
        />
        {/* Preconnect to image CDNs for faster LCP */}
        <link rel="preconnect" href="https://imgd.aeplcdn.com" />
        <link rel="preconnect" href="https://www.cardekho.com" />
        <link rel="preconnect" href="https://logo.clearbit.com" />
        <link rel="dns-prefetch" href="https://imgd.aeplcdn.com" />
      </head>
      <body className="min-h-screen flex flex-col bg-gray-100 antialiased">
        <SiteHeader />
        <main className="flex-1 min-w-0">{children}</main>
        <SiteFooter />
        {/* Lazy-loaded floaters — not needed for first paint */}
        <CompareFloatingBar />
        <AIChatAssistant />
        <BackToTop />
      </body>
    </html>
  );
}
