import { notFound } from "next/navigation";
import { ProductDetailView } from "@/components/product/ProductDetailView";
import { getReviewsForProduct } from "@/data/reviews";
import {
  getProductById,
  getRelatedFromDb,
  listProductIds,
} from "@/lib/catalog";

type Props = { params: Promise<{ id: string }> };

export const dynamic = "force-dynamic";
export const revalidate = 0;

export async function generateStaticParams() {
  const ids = await listProductIds();
  return ids.map((id) => ({ id }));
}

/** Product detail — always from MariaDB when available. */
export default async function ProductDetailPage({ params }: Props) {
  const { id } = await params;

  const product = await getProductById(id);
  if (!product) notFound();

  const [related, reviews] = await Promise.all([
    getRelatedFromDb(product, 5),
    Promise.resolve(getReviewsForProduct(product.id)),
  ]);

  return (
    <ProductDetailView
      product={product}
      related={related}
      reviews={reviews}
    />
  );
}
