import type { Metadata } from "next";
import { Suspense } from "react";
import { notFound } from "next/navigation";
import { getDictionary, getLocale, type Dictionary } from "@/i18n/get-dictionary";
import { products } from "@/content/catalog";
import { fill } from "@/lib/format";
import { Footer } from "@/components/layout/footer/footer";
import { Header } from "@/components/layout/header/header";
import { PageContentPlaceholder } from "@/components/shared/page-content-placeholder";
import { DetailsHeroSection } from "@/features/details/hero/details-hero-section";

// Only products with a Figma details page (Watermelon has none, so it gets no page).
const withDetails = products.filter((product) => product.details);
const findProduct = (slug: string) => withDetails.find((product) => product.slug === slug);

/** The product's details texts, if it has any. */
function detailsText(dict: Dictionary, id: (typeof products)[number]["id"]) {
  const item = dict.catalog.products[id];
  return "details" in item ? item.details : undefined;
}

export function generateStaticParams() {
  return withDetails.map((product) => ({ slug: product.slug }));
}

export async function generateMetadata({ params }: PageProps<"/[lang]/products/[slug]">): Promise<Metadata> {
  const [{ slug }, dict] = await Promise.all([params, getDictionary()]);
  const product = findProduct(slug);
  const text = product && detailsText(dict, product.id);
  return { title: `${text ? text.title : dict.notFound.metaTitle} | ${dict.brand.name}` };
}

/**
 * Product details (Figma "motakamel" — with tablet and phone frames —, "Greeting ways", GLOFairs).
 * The page frame (header, footer) renders right away; everything that depends on the link (`params`) waits inside
 * <Suspense>, as Next.js asks for instant navigation. Links here use `prefetch`, so it is usually ready before the click.
 */
export default async function ProductPage({ params }: PageProps<"/[lang]/products/[slug]">) {
  const [locale, dict] = await Promise.all([getLocale(), getDictionary()]);

  return (
    <>
      <Header locale={locale} nav={dict.nav} activeKey="products" />
      <main>
        <Suspense fallback={<PageContentPlaceholder label={dict.nav.loading} />}>
          <ProductDetails params={params} dict={dict} />
        </Suspense>
      </main>
      {/* Figma: the footer follows the 880px hero directly. */}
      <Footer locale={locale} t={dict.footer} brandName={dict.brand.name} className="mt-0 md:mt-0 lg:mt-0" />
    </>
  );
}

async function ProductDetails({ params, dict }: { params: PageProps<"/[lang]/products/[slug]">["params"]; dict: Dictionary }) {
  const { slug } = await params;
  const product = findProduct(slug);
  const text = product && detailsText(dict, product.id);
  if (!product?.details || !text) notFound();

  return (
    <DetailsHeroSection
      {...text}
      featuresLabel={dict.details.features}
      accent={product.details.accent}
      background={product.details.background}
      mockup={product.details.mockup}
      mockupAlt={fill(dict.details.mockupAlt, { name: text.title })}
    />
  );
}
