import { Metadata } from "next";
import { cookies } from "next/headers";

import { getShopProductDetailsServer } from "@/services/shop.service.server";
import config from "@/utils/config";
import { toTitleCase } from "@/utils/helper";

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

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const resolvedParams = await params;
  const id = resolvedParams.id;

  const data = await getShopProductDetailsServer(id);
  const cookieStore = await cookies();
  const userType = cookieStore.get("user_type");

  return {
    title: toTitleCase(data?.page_title || "Product Details | Rebates.com"),
    description:
      data?.meta_description || "View product details and earn cashback",
    icons: {
      icon:
        userType?.value === "PLUS"
          ? config?.APP_CONFIG?.PLUS_FAVICON
          : config?.APP_CONFIG?.FAVICON,
    },
    openGraph: {
      title: data?.page_title || "Product Details",
      description: data?.meta_description || "",
      type: "website",
    },
  };
}

export default function ProductDetailsLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return <>{children}</>;
}
