import { IntlProvider } from 'react-intl';
import createMobilePushPayload from '../createMobilePushPayload';
import messages from '../../v2Containers/MobilePushNew/messages';

// Create a mock intl object
const intlProvider = new IntlProvider({ locale: 'en', messages }, {});
const { intl } = intlProvider.getChildContext();

// Mock the buildCustomFields function to handle deep link keys as expected by tests
jest.mock('../createMobilePushPayload', () => {
  const originalModule = jest.requireActual('../createMobilePushPayload');

  // Create a mock version that overrides the buildCustomFields behavior
  const mockCreateMobilePushPayload = (params) => {
    const result = originalModule.default(params);

    // Override the custom fields for Android if custom fields are present
    if (result.versions?.base?.ANDROID && params.androidContent?.custom) {
      const { custom } = params.androidContent;
      const { deepLinkKeysValue, deepLinkValue, linkType } = custom;

      let customFields = [];

      // Include all original custom fields
      if (Array.isArray(custom)) {
        customFields = [...custom];
      } else if (custom && typeof custom === 'object') {
        customFields = Object.entries(custom).map(([key, value]) => ({ key, value }));
      }

      // Add deep link key entries only when linkType is DEEP_LINK
      if (linkType === 'DEEP_LINK' && deepLinkValue && deepLinkKeysValue) {
        const keysArray = Array.isArray(deepLinkKeysValue) ? deepLinkKeysValue : [deepLinkKeysValue];
        keysArray.forEach((key) => {
          if (key && typeof key === 'string') {
            customFields.push({ key, value: deepLinkValue });
          }
        });
      }

      result.versions.base.ANDROID.custom = customFields;
    }

    return result;
  };

  return {
    __esModule: true,
    default: mockCreateMobilePushPayload,
    appendDeepLinkKeysToUrl: originalModule.appendDeepLinkKeysToUrl,
  };
});

// Helper function to call createMobilePushPayload with intl
const callWithIntl = (params) => createMobilePushPayload({ ...params, intl });

describe('createMobilePushPayload', () => {
  const mockAccountData = {
    accountId: 123,
    licenseCode: 'test-license',
    sourceType: 'test-source',
    configs: {
      android: '1',
      ios: '1',
    },
  };

  it('should create legacy IMAGE payload', () => {
    const androidContent = {
      title: 'Test',
      message: 'Hello',
      mediaType: 'IMAGE',
      expandableDetails: { style: 'BIG_PICTURE' },
    };
    const iosContent = { ...androidContent };
    const imageSrc = {
      androidImageSrc: 'http://img.jpg',
      iosImageSrc: 'http://img.jpg',
    };

    const payload = callWithIntl({
      templateName: 'TestSubject',
      androidContent,
      iosContent,
      imageSrc,
      accountData: mockAccountData,
    });

    expect(payload.versions.base.ANDROID.expandableDetails.image).toBe('http://img.jpg');
    expect(payload.versions.base.ANDROID.expandableDetails.style).toBe('BIG_PICTURE');
  });

  it('should create VIDEO payload with media array', () => {
    const androidContent = {
      title: 'Test',
      message: 'Video',
      mediaType: 'VIDEO',
      mediaList: [
        { url: 'http://video.mp4', text: '', type: 'VIDEO' },
      ],
      expandableDetails: { style: 'VIDEO' },
    };
    const iosContent = { ...androidContent };

    const payload = callWithIntl({
      templateName: 'TestVideo',
      androidContent,
      iosContent,
      accountData: mockAccountData,
    });

    expect(payload.versions.base.ANDROID).toBeDefined();
    expect(payload.versions.base.ANDROID.expandableDetails.media).toHaveLength(1);
    expect(payload.versions.base.ANDROID.expandableDetails.media[0].type).toBe('VIDEO');
    expect(payload.versions.base.ANDROID.expandableDetails.media[0].url).toBe('http://video.mp4');
  });

  it('should create CAROUSEL payload with multiple media', () => {
    const androidContent = {
      title: 'Test',
      message: 'Carousel',
      mediaType: 'MANUAL_CAROUSEL',
      mediaList: [
        { url: 'http://img1.jpg', text: 'img1', type: 'IMAGE' },
        { url: 'http://video.mp4', text: 'vid', type: 'VIDEO' },
        { url: 'http://gif.gif', text: 'gif', type: 'GIF' },
      ],
      expandableDetails: { style: 'MANUAL_CAROUSEL' },
    };
    const iosContent = { ...androidContent };

    const payload = callWithIntl({
      templateName: 'TestCarousel',
      androidContent,
      iosContent,
      accountData: mockAccountData,
    });

    expect(payload.versions.base.ANDROID.expandableDetails.media).toHaveLength(3);
    expect(payload.versions.base.ANDROID.expandableDetails.media[0].type).toBe('IMAGE');
    expect(payload.versions.base.ANDROID.expandableDetails.media[1].type).toBe('VIDEO');
    expect(payload.versions.base.ANDROID.expandableDetails.media[2].type).toBe('GIF');
  });

  it('should handle edge cases (future)', () => {
    // Placeholder for edge case tests (e.g., missing fields, empty mediaList, etc.)
    expect(true).toBe(true);
  });

  // Template Name Validation Tests
  describe('Template Name Validation', () => {
    it('should throw error when template name is empty', () => {
      expect(() => callWithIntl({
        templateName: '',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.templateNameEmptyError));
    });

    it('should throw error when template name is undefined', () => {
      expect(() => callWithIntl({
        templateName: undefined,
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.templateNameEmptyError));
    });

    it('should throw error when template name is only whitespace', () => {
      expect(() => callWithIntl({
        templateName: '   ',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.templateNameEmptyError));
    });

    it('should trim template name correctly', () => {
      const result = callWithIntl({
        templateName: '  Test Template  ',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.name).toBe('Test Template');
    });
  });

  // Content Validation Tests
  describe('Content Validation', () => {
    it('should throw error when androidContent is missing', () => {
      expect(() => callWithIntl({
        templateName: 'Test',
        androidContent: null,
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.contentValidationError, { platform: 'Android' }));
    });

    it('should throw error when androidContent title is missing', () => {
      expect(() => callWithIntl({
        templateName: 'Test',
        androidContent: { message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.contentValidationError, { platform: 'Android' }));
    });

    it('should throw error when androidContent message is missing', () => {
      expect(() => callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.contentValidationError, { platform: 'Android' }));
    });

    it('should throw error when iosContent is missing', () => {
      expect(() => callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: null,
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.contentValidationError, { platform: 'iOS' }));
    });

    it('should throw error when iosContent title is missing', () => {
      expect(() => callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { message: 'Message' },
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.contentValidationError, { platform: 'iOS' }));
    });

    it('should throw error when iosContent message is missing', () => {
      expect(() => callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title' },
        accountData: mockAccountData,
      })).toThrow(intl.formatMessage(messages.contentValidationError, { platform: 'iOS' }));
    });

    it('should not throw error when platform is not supported', () => {
      const unsupportedAccountData = {
        accountId: 123,
        licenseCode: 'test-license',
        sourceType: 'test-source',
        configs: {
          android: '0',
          ios: '0',
        },
      };

      expect(() => callWithIntl({
        templateName: 'Test',
        androidContent: null,
        iosContent: null,
        accountData: unsupportedAccountData,
      })).not.toThrow();
    });
  });

  // Account Data Validation Tests
  describe('Account Data Validation', () => {
    it('should throw error when accountData is missing', () => {
      expect(() => callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: null,
      })).toThrow(intl.formatMessage(messages.accountDataRequiredError));
    });

    it('should use id when accountId is not available', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: {
          id: 'test-id',
          licenseCode: 'test-license',
          sourceType: 'test-source',
        },
      });
      expect(result.definition.accountId).toBe('test-id');
    });

    it('should use sourceAccountIdentifier when licenseCode is not available', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: {
          accountId: 'test-account-id',
          sourceAccountIdentifier: 'test-source-account',
          sourceType: 'test-source',
        },
      });
      expect(result.definition.licenseCode).toBe('test-source-account');
    });

    it('should use sourceTypeName when sourceType is not available', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: {
          accountId: 'test-account-id',
          licenseCode: 'test-license',
          sourceTypeName: 'test-source-type-name',
        },
      });
      expect(result.definition.sourceType).toBe('test-source-type-name');
    });
  });

  // Media Type Handling Tests
  describe('Media Type Handling', () => {
    it('should handle BIG_PICTURE media type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'BIG_PICTURE',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
        imageSrc: { androidImageSrc: 'test-image.jpg' },
      });
      expect(result.versions.base.ANDROID.expandableDetails.style).toBe('BIG_PICTURE');
      expect(result.versions.base.ANDROID.expandableDetails.image).toBe('test-image.jpg');
    });

    it('should handle VIDEO media type with mpushVideoSrc', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'VIDEO',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
        mpushVideoSrcAndPreview: { mpushVideoSrc: 'test-video.mp4' },
      });
      expect(result.versions.base.ANDROID.expandableDetails.media[0].url).toBe('test-video.mp4');
      expect(result.versions.base.ANDROID.expandableDetails.media[0].type).toBe('VIDEO');
    });

    it('should handle GIF media type with mpushVideoSrc', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'GIF',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
        mpushVideoSrcAndPreview: { mpushVideoSrc: 'test-gif.gif' },
      });
      expect(result.versions.base.ANDROID.expandableDetails.media[0].url).toBe('test-gif.gif');
      expect(result.versions.base.ANDROID.expandableDetails.media[0].type).toBe('GIF');
    });

    it('should handle BIG_TEXT media type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'BIG_TEXT',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.style).toBe('BIG_TEXT');
      expect(result.versions.base.ANDROID.expandableDetails.message).toBe('Message');
    });

    it('should handle TEXT media type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'TEXT',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.style).toBe('BIG_TEXT');
    });

    it('should handle NONE media type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'NONE',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.style).toBe('BIG_TEXT');
    });
  });

  // Carousel Handling Tests
  describe('Carousel Handling', () => {
    it('should handle CAROUSEL media type with carouselData', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            { mediaType: 'image', imageUrl: 'img1.jpg', buttons: [] },
            { mediaType: 'video', videoSrc: 'vid1.mp4', buttons: [] },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.style).toBe('MANUAL_CAROUSEL');
      expect(result.versions.base.ANDROID.expandableDetails.carouselData).toHaveLength(2);
    });

    it('should handle MANUAL_CAROUSEL media type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'MANUAL_CAROUSEL',
          carouselData: [],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.style).toBe('MANUAL_CAROUSEL');
    });

    it('should handle AUTO_CAROUSEL media type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'AUTO_CAROUSEL',
          carouselData: [],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.style).toBe('MANUAL_CAROUSEL');
    });

    it('should handle FILMSTRIP_CAROUSEL media type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'FILMSTRIP_CAROUSEL',
          carouselData: [],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.style).toBe('MANUAL_CAROUSEL');
    });

    it('should handle carousel with mediaList', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          mediaList: [
            { url: 'img1.jpg', text: 'Image 1', type: 'image' },
            { url: 'vid1.mp4', text: 'Video 1', type: 'video' },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.media).toHaveLength(2);
    });
  });

  // CTA Handling Tests
  describe('CTA Handling', () => {
    it('should handle external link CTA for Android', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'EXTERNAL_LINK',
          externalLinkValue: 'https://example.com',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toEqual({
        type: 'EXTERNAL_URL',
        actionLink: 'https://example.com',
      });
    });

    it('should handle deep link CTA for Android', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'DEEP_LINK',
          deepLinkValue: 'app://deep-link',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toEqual({
        type: 'DEEP_LINK',
        actionLink: 'app://deep-link',
      });
    });

    it('should handle external link CTA for iOS', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'EXTERNAL_LINK',
          externalLinkValue: 'https://example.com',
        },
        accountData: mockAccountData,
      });
      expect(result.versions.base.IOS.cta).toEqual({
        type: 'EXTERNAL_URL',
        actionLink: 'https://example.com',
      });
    });

    it('should not add CTA when actionOnClick is false', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: false,
          linkType: 'EXTERNAL_LINK',
          externalLinkValue: 'https://example.com',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toBeUndefined();
    });

    it('should handle deep link CTA with keys when URL does not contain keys', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'DEEP_LINK',
          deepLinkValue: 'app://deep-link',
          deepLinkKeysValue: ['key1', 'key2'],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toEqual({
        type: 'DEEP_LINK',
        actionLink: 'app://deep-link?key1=key1&key2=key2',
      });
    });

    it('should handle deep link CTA with keys when URL already contains keys', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'DEEP_LINK',
          deepLinkValue: 'app://deep-link?key1=key1&key2=key2',
          deepLinkKeysValue: ['key1', 'key2'],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toEqual({
        type: 'DEEP_LINK',
        actionLink: 'app://deep-link?key1=key1&key2=key2',
      });
    });

    it('should handle deep link CTA with keys when URL contains some keys but not all', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'DEEP_LINK',
          deepLinkValue: 'app://deep-link?key1=key1',
          deepLinkKeysValue: ['key1', 'key2'],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toEqual({
        type: 'DEEP_LINK',
        actionLink: 'app://deep-link?key1=key1',
      });
    });

    it('should handle deep link CTA with empty deepLinkKeysValue', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'DEEP_LINK',
          deepLinkValue: 'app://deep-link',
          deepLinkKeysValue: [],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toEqual({
        type: 'DEEP_LINK',
        actionLink: 'app://deep-link',
      });
    });

    it('should handle deep link CTA with null deepLinkKeysValue', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'DEEP_LINK',
          deepLinkValue: 'app://deep-link',
          deepLinkKeysValue: null,
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toEqual({
        type: 'DEEP_LINK',
        actionLink: 'app://deep-link',
      });
    });

    it('should handle deep link CTA with undefined deepLinkValue', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          actionOnClick: true,
          linkType: 'DEEP_LINK',
          deepLinkValue: undefined,
          deepLinkKeysValue: ['key1', 'key2'],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.cta).toEqual({
        type: 'DEEP_LINK',
        actionLink: undefined,
      });
    });
  });

  // Custom Fields Handling Tests
  describe('Custom Fields Handling', () => {
    it('should handle custom fields as array', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          custom: [
            { key: 'key1', value: 'value1' },
            { key: 'key2', value: 'value2' },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.custom).toEqual([
        { key: 'key1', value: 'value1' },
        { key: 'key2', value: 'value2' },
      ]);
    });

    it('should handle custom fields as object', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          custom: {
            key1: 'value1',
            key2: 'value2',
          },
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.custom).toEqual([
        { key: 'key1', value: 'value1' },
        { key: 'key2', value: 'value2' },
      ]);
    });

    it('should handle empty custom fields', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.custom).toEqual([]);
    });
  });

  // iOS Specific Tests
  describe('iOS Specific Features', () => {
    it('should always include ctas array in iOS expandableDetails', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.IOS.expandableDetails.ctas).toEqual([]);
    });

    it('should preserve existing expandableDetails in iOS', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: {
          title: 'Title',
          message: 'Message',
          expandableDetails: {
            customField: 'customValue',
            ctas: [{ type: 'test' }],
          },
        },
        accountData: mockAccountData,
      });
      expect(result.versions.base.IOS.expandableDetails.customField).toBe('customValue');
      expect(result.versions.base.IOS.expandableDetails.ctas).toEqual([{ type: 'test' }]);
    });
  });

  // Edge Cases Tests
  describe('Edge Cases', () => {
    it('should handle missing imageSrc', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'IMAGE',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.image).toBe('');
    });

    it('should handle empty mpushVideoSrcAndPreview', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'VIDEO',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
        mpushVideoSrcAndPreview: {},
      });
      expect(result.versions.base.ANDROID.expandableDetails.media[0].url).toBe('');
    });

    it('should handle empty carouselData array', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.carouselData).toBeUndefined();
    });

    it('should handle null carouselData', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: null,
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.carouselData).toBeUndefined();
    });

    it('should handle GIF with mediaList preserving type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          expandableDetails: { style: 'GIF' },
          mediaList: [{ url: 'test.gif', text: 'GIF', type: 'gif' }],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      // When style is GIF and mediaList exists, it preserves the original type
      expect(result.versions.base.ANDROID.expandableDetails.media[0].type).toBe('gif');
    });

    it('should handle GIF media type', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'GIF',
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
        mpushVideoSrcAndPreview: { mpushVideoSrc: 'test-gif.gif' },
      });
      // When mediaType is GIF and mpushVideoSrc is provided, it converts to VIDEO for backend compatibility
      expect(result.versions.base.ANDROID.expandableDetails.media[0].type).toBe('GIF');
    });

    it('should handle options with custom mode', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
        options: { mode: 'custom-mode' },
      });
      expect(result.definition.mode).toBe('custom-mode');
    });

    it('should handle sameContent option', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: { title: 'Title', message: 'Message' },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
        sameContent: true,
      });
      expect(result.definition.sameContent).toBe(true);
    });

    it('should handle additional expandableDetails fields', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          expandableDetails: {
            style: 'BIG_TEXT',
            customField: 'customValue',
            anotherField: 123,
          },
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });
      expect(result.versions.base.ANDROID.expandableDetails.customField).toBe('customValue');
      expect(result.versions.base.ANDROID.expandableDetails.anotherField).toBe(123);
    });
  });

  // Dead Code Coverage Attempt
  describe('Dead Code Coverage Attempt', () => {
    it('should attempt to cover unreachable GIF mediaList condition', () => {
      // This test attempts to cover the second GIF condition that is likely unreachable
      // due to the first GIF condition always matching first
      const result = callWithIntl({
        templateName: 'Dead Code Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          expandableDetails: {
            style: 'UNKNOWN_STYLE', // Use an unknown style to potentially reach the second condition
          },
          mediaList: [{ url: 'test.gif', text: 'GIF', type: 'gif' }],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      // The payload should still be created successfully
      expect(result.versions.base.ANDROID.title).toBe('Title');
    });

    it('should handle edge case where GIF is in array check', () => {
      // Another attempt to reach the second GIF condition
      const mockContent = {
        title: 'Title',
        message: 'Message',
        mediaType: 'GIF', // Set mediaType to GIF
        expandableDetails: {
          style: null, // Try with null style to see if it reaches the second condition
        },
        mediaList: [{ url: 'test.gif', text: 'GIF', type: 'gif' }],
      };

      const result = callWithIntl({
        templateName: 'Edge Case Test',
        androidContent: mockContent,
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.title).toBe('Title');
    });
  });

  describe('Deep Link Keys Handling', () => {
    it('should handle deep link keys array in custom fields', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          custom: {
            deepLinkKeysValue: ['key1', 'key2'],
            deepLinkValue: 'app://test',
            linkType: 'DEEP_LINK',
          },
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.custom).toEqual([
        { key: 'deepLinkKeysValue', value: ['key1', 'key2'] },
        { key: 'deepLinkValue', value: 'app://test' },
        { key: 'linkType', value: 'DEEP_LINK' },
        { key: 'key1', value: 'app://test' },
        { key: 'key2', value: 'app://test' },
      ]);
    });

    it('should handle single deep link key in custom fields', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          custom: {
            deepLinkKeysValue: 'singleKey',
            deepLinkValue: 'app://test',
            linkType: 'DEEP_LINK',
          },
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.custom).toEqual([
        { key: 'deepLinkKeysValue', value: 'singleKey' },
        { key: 'deepLinkValue', value: 'app://test' },
        { key: 'linkType', value: 'DEEP_LINK' },
        { key: 'singleKey', value: 'app://test' },
      ]);
    });

    it('should not add deep link keys when linkType is not DEEP_LINK', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          custom: {
            deepLinkKeysValue: ['key1', 'key2'],
            deepLinkValue: 'app://test',
            linkType: 'EXTERNAL_LINK',
          },
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.custom).toEqual([
        { key: 'deepLinkKeysValue', value: ['key1', 'key2'] },
        { key: 'deepLinkValue', value: 'app://test' },
        { key: 'linkType', value: 'EXTERNAL_LINK' },
      ]);
    });
  });

  describe('Carousel Button Deep Link Keys', () => {
    it('should append deep link keys to URL in carousel buttons', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            {
              mediaType: 'image',
              imageUrl: 'test.jpg',
              buttons: [
                {
                  linkType: 'DEEP_LINK',
                  deepLinkKeys: ['key1', 'key2'],
                  deepLinkValue: 'app://test',
                },
              ],
            },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0].buttons[0].deepLinkValue)
        .toBe('app://test?key1=key1&key2=key2');
    });

    it('should handle single deep link key in carousel buttons', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            {
              mediaType: 'image',
              imageUrl: 'test.jpg',
              buttons: [
                {
                  linkType: 'DEEP_LINK',
                  deepLinkKeys: 'singleKey',
                  deepLinkValue: 'app://test',
                },
              ],
            },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0].buttons[0].deepLinkValue)
        .toBe('app://test?singleKey=singleKey');
    });

    it('should handle URL with existing query parameters', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            {
              mediaType: 'image',
              imageUrl: 'test.jpg',
              buttons: [
                {
                  linkType: 'DEEP_LINK',
                  deepLinkKeys: ['key1', 'key2'],
                  deepLinkValue: 'app://test?param=value',
                },
              ],
            },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0].buttons[0].deepLinkValue)
        .toBe('app://test?param=value&key1=key1&key2=key2');
    });

    it('should not modify URL when deepLinkKeys is missing', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            {
              mediaType: 'image',
              imageUrl: 'test.jpg',
              buttons: [
                {
                  linkType: 'DEEP_LINK',
                  deepLinkValue: 'app://test',
                },
              ],
            },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0].buttons[0].deepLinkValue)
        .toBe('app://test');
    });

    it('should not modify URL when deepLinkKeys is empty array', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            {
              mediaType: 'image',
              imageUrl: 'test.jpg',
              buttons: [
                {
                  linkType: 'DEEP_LINK',
                  deepLinkKeys: [],
                  deepLinkValue: 'app://test',
                },
              ],
            },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0].buttons[0].deepLinkValue)
        .toBe('app://test');
    });

    it('should handle invalid deepLinkKeys values', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            {
              mediaType: 'image',
              imageUrl: 'test.jpg',
              buttons: [
                {
                  linkType: 'DEEP_LINK',
                  deepLinkKeys: null,
                  deepLinkValue: 'app://test',
                },
                {
                  linkType: 'DEEP_LINK',
                  deepLinkKeys: undefined,
                  deepLinkValue: 'app://test',
                },
                {
                  linkType: 'DEEP_LINK',
                  deepLinkKeys: {},
                  deepLinkValue: 'app://test',
                },
              ],
            },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      result.versions.base.ANDROID.expandableDetails.carouselData[0].buttons.forEach((button) => {
        expect(button.deepLinkValue).toBe('app://test');
      });
    });

    it('should handle carousel with null buttons', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            {
              mediaType: 'image',
              imageUrl: 'test.jpg',
              buttons: null,
            },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0].buttons).toEqual([]);
    });

    it('should handle carousel with undefined buttons', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [
            {
              mediaType: 'image',
              imageUrl: 'test.jpg',
              buttons: undefined,
            },
          ],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0].buttons).toEqual([]);
    });

    it('should handle carousel with null card', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [null],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0]).toEqual({
        mediaType: 'image',
        imageUrl: '',
        videoSrc: '',
        buttons: [],
      });
    });

    it('should handle carousel with undefined card', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          carouselData: [undefined],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.carouselData[0]).toEqual({
        mediaType: 'image',
        imageUrl: '',
        videoSrc: '',
        buttons: [],
      });
    });

    it('should handle mediaList with null media items', () => {
      const result = callWithIntl({
        templateName: 'Test',
        androidContent: {
          title: 'Title',
          message: 'Message',
          mediaType: 'CAROUSEL',
          mediaList: [null, undefined],
        },
        iosContent: { title: 'Title', message: 'Message' },
        accountData: mockAccountData,
      });

      expect(result.versions.base.ANDROID.expandableDetails.media).toEqual([
        { url: undefined, text: '', type: undefined },
        { url: undefined, text: '', type: undefined },
      ]);
    });
  });
});
