import React from "react";
import type { Metadata } from "next";
import Navbar from "@/components/Navbar";
import Hero from "@/components/Hero";
import CategoryShowcase from "@/components/CategoryShowcase";
import FeaturedProducts from "@/components/FeaturedProducts";
import BrandStory from "@/components/BrandStory";
import ExportNetwork from "@/components/ExportNetwork";
import ContactSection from "@/components/ContactSection";
import ResourcesSection from "@/components/ResourcesSection";
import Footer from "@/components/Footer";
import CinematicMotionProvider from "@/components/CinematicMotionProvider";
import JsonLdScript from "@/components/JsonLdScript";
import { prisma } from "@/lib/db";
import { getTranslations } from "next-intl/server";
import { buildLocalizedPageMetadata } from "@/lib/seo";
import { buildHomeStructuredData } from "@/lib/public-page-seo";
import { selectMixedFeaturedProducts } from "@/lib/featured-products";
import { getEnabledExportMarkets, getHeroFilms, getPublishedCollections, getSiteSectionState } from "@/lib/admin-content";

export const revalidate = 0;

export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }): Promise<Metadata> {
  const { locale } = await params;
  const t = await getTranslations({ locale, namespace: "meta" });
  return buildLocalizedPageMetadata({
    locale,
    pathname: "",
    title: t("title"),
    description: t("description"),
    image: "/media/story/catalog/catalog-cover-1080.webp",
    imageAlt: `${t("title")} — Norden`,
  });
}

export default async function Home({
  params,
}: {
  params: Promise<{ locale: string }>;
}) {
  const { locale } = await params;
  const meta = await getTranslations({ locale, namespace: "meta" });
  const structuredData = buildHomeStructuredData({
    locale,
    name: `${meta("title")} — Norden`,
    description: meta("description"),
  });

  const [productsRaw, collections, heroFilms, markets, sections] = await Promise.all([
    prisma.product.findMany({
      where: { status: "published" },
      select: {
        id: true,
        slug: true,
        sku: true,
        name: true,
        nameTr: true,
        nameEn: true,
        nameRu: true,
        widthCm: true,
        surface: true,
        burnerCount: true,
        description: true,
        descriptionTr: true,
        descriptionEn: true,
        descriptionRu: true,
        images: {
          orderBy: { order: "asc" },
          take: 1,
          select: { id: true, url: true, alt: true, order: true },
        },
        series: { select: { id: true, name: true } },
        category: { select: { id: true, name: true } },
      },
      orderBy: [{ seriesId: "asc" }, { order: "asc" }],
    }),
    getPublishedCollections(),
    getHeroFilms(),
    getEnabledExportMarkets(),
    getSiteSectionState(),
  ]);
  const products = selectMixedFeaturedProducts(productsRaw, (product) => product.series?.name || "NORDEN");
  const sectionContent: Record<string, React.ReactNode> = {
    hero: <Hero locale={locale} films={heroFilms} />,
    collections: <CategoryShowcase collections={collections} />,
    "featured-products": <FeaturedProducts products={products} />,
    about: <BrandStory />,
    "export-network": <ExportNetwork markets={markets} />,
    resources: <ResourcesSection />,
    contact: <ContactSection />,
  };

  return (
    <div
      className="min-h-screen flex flex-col font-sans"
      style={{ background: "var(--surface-0)", color: "var(--fg-primary)" }}
    >
      <JsonLdScript data={structuredData} />
      <Navbar logoUrl="/brand/norden.png" locale={locale} />
      <CinematicMotionProvider>
        {sections.filter((section) => section.enabled).map((section) => (
          <React.Fragment key={section.key}>{sectionContent[section.key] || null}</React.Fragment>
        ))}
      </CinematicMotionProvider>
      <Footer />
    </div>
  );
}
