/**
 * CMS ELEMENT: product-description-reviews
 * Shopware element type `product-description-reviews` — the tabbed
 * description + reviews block on a product detail CMS layout. Reads the
 * resolved product (and any pre-fetched reviews) from `slot.data`.
 */

import ProductDetailTabs from '@/components/product/ProductDetailTabs';
import { getProductProperties, type ShopwareProduct, type ProductReview } from '@/lib/shopware-api';
import type { CmsSlot } from '@/components/cms/types';

export default function CmsElementProductDescriptionReviews({ slot }: { slot: CmsSlot }) {
  const product: ShopwareProduct | undefined = slot.data?.product;
  if (!product) return null;

  const name = product.translated?.name || product.name;
  const description = product.translated?.description || product.description || null;
  const properties = getProductProperties(product);

  // Reviews may be resolved into the slot (Store API) or injected by
  // resolveProductCmsSlots; otherwise ProductReviews lazy-loads them client-side.
  const reviewsData = slot.data?.reviews;
  const initialReviews: ProductReview[] = reviewsData?.elements ?? [];
  const initialTotal: number = reviewsData?.total ?? initialReviews.length;

  return (
    <ProductDetailTabs
      productId={product.id}
      productName={name}
      description={description}
      properties={properties}
      initialReviews={initialReviews}
      initialTotal={initialTotal}
      ratingAverage={product.ratingAverage ?? null}
    />
  );
}
