import get from 'lodash/get';

/**
 * Vendor-specific data transformers
 * Handles different vendor data structures without affecting existing components
 */

export const VENDOR_TYPES = {
  KARIX: 'karixwhatsappbulk',
  GUPSHUP: 'gupshupwhatsappbulk',
  HAPTIC: 'hapticwhatsappbulk',
  TWILIO: 'twiliowhatsapptrans',
};

/**
 * Transform vendor-specific asset data to standardized format
 */
export const transformAssetData = (assetData, vendorType) => {
  if (!assetData) return {};

  switch (vendorType) {
    case VENDOR_TYPES.HAPTIC:
      return transformHapticAssetData(assetData);
    case VENDOR_TYPES.KARIX:
    case VENDOR_TYPES.GUPSHUP:
    default:
      return transformStandardAssetData(assetData);
  }
};

/**
 * Transform HAPTIC vendor data structure
 */
const transformHapticAssetData = (assetData) => {
  const metaInfo = get(assetData, 'metaInfo', {});
  return {
    videoSrc: get(metaInfo, 'secure_file_path', '') || get(assetData, 'videoSrc', ''),
    previewUrl: get(assetData, 'previewUrl', ''),
    videoName: get(metaInfo, 'name', ''),
    videoDuration: get(metaInfo, 'duration', ''),
    fileHandle: get(metaInfo, 'hapticFileHandle', ''),
    videoHeight: get(metaInfo, 'height', ''),
    videoWidth: get(metaInfo, 'width', ''),
  };
};

/**
 * Transform standard vendor data structure (Karix, Gupshup, etc.)
 */
const transformStandardAssetData = (assetData) => ({
  videoSrc: get(assetData, 'videoSrc', ''),
  previewUrl: get(assetData, 'previewUrl', ''),
  videoName: get(assetData, 'videoName', ''),
  videoDuration: get(assetData, 'videoDuration', ''),
  fileHandle: get(assetData, 'karixFileHandle', ''),
  videoHeight: get(assetData, 'videoHeight', ''),
  videoWidth: get(assetData, 'videoWidth', ''),
});

/**
 * Transform standardized data back to vendor-specific format
 */
export const transformToVendorFormat = (standardData, vendorType) => {
  switch (vendorType) {
    case VENDOR_TYPES.HAPTIC:
      return {
        metaInfo: {
          secure_file_path: standardData.videoSrc,
          name: standardData.videoName,
          duration: standardData.videoDuration,
          hapticFileHandle: standardData.fileHandle,
          height: standardData.videoHeight,
          width: standardData.videoWidth,
        },
        previewUrl: standardData.previewUrl,
      };
    case VENDOR_TYPES.KARIX:
    case VENDOR_TYPES.GUPSHUP:
    default:
      return {
        videoSrc: standardData.videoSrc,
        previewUrl: standardData.previewUrl,
        videoName: standardData.videoName,
        videoDuration: standardData.videoDuration,
        karixFileHandle: standardData.fileHandle,
        videoHeight: standardData.videoHeight,
        videoWidth: standardData.videoWidth,
      };
  }
};

/**
 * Get file handle based on vendor type
 */
export const getFileHandle = (assetData, vendorType) => {
  if (!assetData) return '';

  switch (vendorType) {
    case VENDOR_TYPES.HAPTIC:
      return get(assetData, 'metaInfo.hapticFileHandle', '');
    case VENDOR_TYPES.KARIX:
    case VENDOR_TYPES.GUPSHUP:
    default:
      return get(assetData, 'karixFileHandle', '') || get(assetData, 'metaInfo.karixFileHandle', '');
  }
};

/**
 * Resolve vendor-specific carousel video update fields
 * Handles different vendor data structures (HAPTIC, KARIX, GUPSHUP) for carousel video updates
 *
 * @param {Object} params - Parameters object
 * @param {string} params.host - Vendor host type (HOST_HAPTIC, HOST_KARIX, HOST_GUPSHUP)
 * @param {Object} params.data - Asset data object with vendor-specific structure
 * @param {string} params.videoSrc - Standard video source (fallback)
 * @param {string} params.previewUrl - Standard preview URL (fallback)
 * @param {string} params.fileHandle - Standard file handle (fallback)
 * @returns {Object} Object containing resolved values and carousel update fields array
 */
export const resolveCarouselVideoUpdateFields = ({
  host,
  data,
  videoSrc = '',
  previewUrl = '',
  fileHandle = '',
}) => {
  const HOST_HAPTIC = VENDOR_TYPES.HAPTIC;
  const HOST_KARIX = VENDOR_TYPES.KARIX;
  const HOST_GUPSHUP = VENDOR_TYPES.GUPSHUP;

  const resolvedVideoSrc = host === HOST_HAPTIC
    ? (data?.metaInfo?.secure_file_path || videoSrc)
    : videoSrc;

  const resolvedPreviewUrl = host === HOST_HAPTIC
    ? (data?.previewUrl || previewUrl)
    : previewUrl;

  const resolvedHapticHandle = host === HOST_HAPTIC
    ? (data?.metaInfo?.hapticFileHandle || fileHandle)
    : '';

  const resolvedKarixHandle = (host === HOST_KARIX || host === HOST_GUPSHUP)
    ? fileHandle
    : '';

  const standardAsset = host === HOST_HAPTIC
    ? transformAssetData({ ...data, metaInfo: data?.metaInfo }, VENDOR_TYPES.HAPTIC)
    : data;

  const updateFields = [
    { fieldName: "videoSrc", value: resolvedVideoSrc },
    { fieldName: "videoPreviewImg", value: resolvedPreviewUrl },
    { fieldName: "assetList", value: standardAsset },
    { fieldName: "hapticFileHandle", value: resolvedHapticHandle },
    { fieldName: "karixFileHandle", value: resolvedKarixHandle },
  ];

  return {
    resolvedVideoSrc,
    resolvedPreviewUrl,
    resolvedHapticHandle,
    resolvedKarixHandle,
    standardAsset,
    updateFields,
  };
};
