import type { Metadata } from "next";
import { notFound } from "next/navigation";
import ProductDetail from "@/components/products/ProductDetail";
import { products } from "@/lib/products";

// Only the products in lib/products.ts exist; any other id is a real 404.
export const dynamicParams = false;

export function generateStaticParams() {
    return products.map((product) => ({ id: product.id }));
}

export async function generateMetadata({ params }: { params: Promise<{ id: string }> }): Promise<Metadata> {
    const { id } = await params;
    const product = products.find((p) => p.id === id);

    if (!product) {
        return { title: "Product Not Found" };
    }

    const description = product.longDesc.substring(0, 155) + "...";

    return {
        // Absolute: the "Products" title in app/products/layout.tsx would otherwise drop the "| iRoof" suffix.
        title: { absolute: `${product.name} | iRoof` },
        description,
        alternates: { canonical: `/products/${product.id}` },
        openGraph: {
            title: `${product.name} | iRoof`,
            description,
            images: [product.image],
        },
    };
}

export default async function ProductDetailPage({ params }: { params: Promise<{ id: string }> }) {
    const { id } = await params;

    if (!products.some((p) => p.id === id)) {
        notFound();
    }

    return <ProductDetail productId={id} />;
}
