import { notFound } from "next/navigation";
import { db } from "@/lib/db";
import ProductDetailClient from "./ProductDetailClient";

type Params = { slug: string };

export async function generateMetadata({ params }: { params: Promise<Params> }) {
  const { slug } = await params;
  const product = await db.product.findFirst({ where: { slug, isActive: true }, select: { name: true, description: true } });
  if (!product) return { title: "Product Not Found" };
  return { title: product.name, description: product.description ?? undefined };
}

export default async function ProductDetailPage({ params }: { params: Promise<Params> }) {
  const { slug } = await params;
  const product = await db.product.findFirst({
    where: { slug, isActive: true },
    include: {
      images: { orderBy: { sortOrder: "asc" } },
      variations: { orderBy: { createdAt: "asc" } },
      category: true,
    },
  });

  if (!product) notFound();

  return <ProductDetailClient product={product as never} />;
}
