import type { ProductRetrievalResponse } from '@spiffy-ai/commerce-api-client';
import { transformSnakeToCamel } from 'src/application/models/utils/snakeToCamelTransformer';
import type { SearchResponseProductAttributes } from 'src/application/models/api/response';
import type { ProductRetrievalResult } from 'src/atoms/search/types';

/**
 * Maps the API response from /v1/products/retrieve to the core ProductRetrievalResult.
 * Converts product attributes from snake_case to camelCase to match SearchResponseProductAttributes.
 */
export function productRetrievalResponseToProductRetrievalResult(
  apiResponse: ProductRetrievalResponse,
): ProductRetrievalResult {
  const products = (apiResponse.products ?? []).map(product =>
    transformSnakeToCamel(product),
  ) as SearchResponseProductAttributes['attributes'][];

  return {
    products,
    totalProductCount: products.length,
  };
}
