const { sellerProductImageBuilder } = require("../helpers/image-src-builder");
const { checkPOItemStock } = require("../services/purchase-order.service");

const productMapper = async (sellerProducts, enabledPO = false) => {
  let __sellerProducts = Array.isArray(sellerProducts)
    ? sellerProducts
    : [sellerProducts];

  const _sellerProducts = await Promise.all(
    __sellerProducts.map(
      async ({
        id,
        menu,
        description,
        price,
        discounted_price,
        photo,
        quantity_limit,
        stocks,
        available,
        sku,
        menus,
        seller_dashboard_src,
        product_variations = [],
        product_images = [],
      }) => {
        const poStocks = enabledPO ? await checkPOItemStock(id) : 0;
        let itemAvailableStocks =
          poStocks && poStocks.availableStock ? poStocks.availableStock : 0;
        const response = {
          id,
          name: menu,
          description,
          price,
          discounted_price,
          photo: [],
          quantity_limit,
          stocks,
          available,
          sku,
          category: menus,
          seller_dashboard_src,
          poStocks: itemAvailableStocks,
        };

        // Main photo
        if (photo) {
          response.photo.push({
            photo: sellerProductImageBuilder(photo, seller_dashboard_src),
          });
        }

        // Additional product images
        product_images.forEach(({ id, file_name, is_thumbnail }) => {
          if (file_name) {
            response.photo.push({
              id,
              photo: sellerProductImageBuilder(file_name, true),
              is_thumbnail,
            });
          }
        });

        // Handle variations (async)
        const productVariation = await Promise.all(
          product_variations.map(
            async ({
              id,
              variation,
              description,
              quantity_limit,
              discounted_price,
              hasSubVariation,
              isAvailable,
              variationImage,
              sku,
              stocks,
              ProductSubVariations = [],
            }) => {
              const variationPoStocks = enabledPO
                ? await checkPOItemStock(id)
                : 0;

              const variationResponse = {
                id,
                name: variation,
                description,
                quantity_limit,
                discounted_price,
                has_sub_variation: hasSubVariation,
                is_available: isAvailable,
                photo: variationImage
                  ? sellerProductImageBuilder(variationImage, true)
                  : null,
                sku,
                stocks,
                variationPoStocks,
              };

              // Sub-variations (sync)
              variationResponse.product_sub_variations =
                ProductSubVariations.map(
                  ({
                    id,
                    custom,
                    description,
                    price,
                    discounted_price,
                    quantity_limit,
                    stocks,
                    sku,
                    isAvailable,
                    photo,
                  }) => ({
                    id,
                    name: custom,
                    description,
                    price,
                    discounted_price,
                    quantity_limit,
                    stocks,
                    sku,
                    is_available: isAvailable,
                    photo: photo
                      ? sellerProductImageBuilder(photo, true)
                      : null,
                  })
                );

              return variationResponse;
            }
          )
        );

        response.product_variations = productVariation;
        return response;
      }
    )
  );

  return _sellerProducts;
};

module.exports = productMapper;
