import { renderHook, act } from '@testing-library/react';
import useUpload from '../useUpload';
import {
  ANDROID,
  IOS,
  MOBILE_PUSH_CHANNEL,
  MPUSH_IMG_SIZE,
  MPUSH_VIDEO_SIZE,
  MPUSH_GIF_SIZE,
  IMAGE,
  VIDEO,
  GIF,
  CAROUSEL,
} from '../../constants';

// Mock cdnTransformation module
jest.mock('../../../../utils/cdnTransformation', () => ({
  getCdnUrl: ({ url, channelName }) => {
    if (!url) return '';
    return `cdn/${channelName}/${url}`;
  },
  getLocalStorageItem: (key, parse = false) => {
    const mockData = {
      CREATIVES_CDN_BASE_URL: 'https://cdn.example.com',
      CREATIVES_CDN_QUALITY_CONFIG: '{"MOBILEPUSH":75,"DEFAULT":80}',
      CREATIVES_CDN_TRANSFORMATION_URL_SUFFIX: 'cdn-cgi/image',
      CREATIVES_S3_BUCKET_PATH: 'intouch_creative_assets',
    };
    const val = mockData[key];
    if (parse && val) {
      return JSON.parse(val);
    }
    return val;
  },
}));

describe('useUpload Hook', () => {
  // Mock data and functions
  let mockMobilePushActions;
  let androidContent;
  let iosContent;
  let mockSetAndroidContent;
  let mockSetIosContent;

// Mock formatMessage function
  const mockFormatMessage = (message, values = {}) => {
  if (message.id === 'app.containers.MobilePushNew.fileSizeError') {
      return `File size should be less than ${values.size}MB`;
  }
  if (message.id === 'app.containers.MobilePushNew.imageFileTypeError') {
      return 'Only JPEG and PNG files are allowed';
  }
  if (message.id === 'app.containers.MobilePushNew.videoFileTypeError') {
      return 'Only 3GP, MP4, MOV, M4V files are allowed';
  }
  if (message.id === 'app.containers.MobilePushNew.gifFileTypeError') {
      return 'Only GIF files are allowed';
  }
  return message.defaultMessage;
};

  // Helper function to create hook instance
  const createHook = (props = {}) => renderHook(() => useUpload(
    mockMobilePushActions,
    props.editData || {},
    props.uploadedAssetData0 || {},
    props.uploadedAssetData1 || {},
    props.uploadAssetSuccess || false,
    props.sameContent || false,
    mockSetAndroidContent,
    mockSetIosContent,
    props.activeTab || ANDROID,
    androidContent,
    iosContent,
    mockFormatMessage,
  ));

  beforeEach(() => {
    mockMobilePushActions = {
    uploadAsset: jest.fn(),
    clearAsset: jest.fn(),
  };

    androidContent = { title: 'Test Android', message: 'Test message' };
    iosContent = { title: 'Test iOS', message: 'Test message' };

    mockSetAndroidContent = jest.fn((updater) => {
      if (typeof updater === 'function') {
        androidContent = updater(androidContent);
      } else {
        androidContent = { ...androidContent, ...updater };
      }
    });

    mockSetIosContent = jest.fn((updater) => {
      if (typeof updater === 'function') {
        iosContent = updater(iosContent);
      } else {
        iosContent = { ...iosContent, ...updater };
      }
    });

    // Reset all mocks
    jest.clearAllMocks();
  });

  describe('Asset Upload', () => {
    it('should handle image upload with validation', () => {
      const { result } = createHook();

      // Test valid image upload
      const validFile = new File([''], 'test.jpg', { type: 'image/jpeg' });
      Object.defineProperty(validFile, 'size', { value: MPUSH_IMG_SIZE - 1000 }); // Just under limit
      act(() => {
        result.current.uploadMpushAsset(validFile, IMAGE.toLowerCase(), {});
      });
      expect(mockMobilePushActions.uploadAsset).toHaveBeenCalledWith(
        validFile,
        IMAGE.toLowerCase(),
        {},
        0,
        { source: MOBILE_PUSH_CHANNEL.toLowerCase(), channel: MOBILE_PUSH_CHANNEL }
      );

      // Test invalid image upload
      const invalidFile = new File([''], 'test.txt', { type: 'text/plain' });
      act(() => {
        result.current.uploadMpushAsset(invalidFile, IMAGE.toLowerCase(), {});
      });
      expect(result.current.uploadErrors[0]).toEqual(['Only JPEG and PNG files are allowed']);
    });

    it('should handle video upload with validation', () => {
      const { result } = createHook();

      // Test valid video upload
      const validFile = new File([''], 'test.mp4', { type: 'video/mp4' });
      Object.defineProperty(validFile, 'size', { value: MPUSH_VIDEO_SIZE - 1000 }); // Just under limit
      act(() => {
        result.current.uploadMpushAsset(validFile, VIDEO.toLowerCase(), {});
      });
      expect(mockMobilePushActions.uploadAsset).toHaveBeenCalledWith(
        validFile,
        VIDEO.toLowerCase(),
        {},
        0,
        { source: MOBILE_PUSH_CHANNEL.toLowerCase(), channel: MOBILE_PUSH_CHANNEL }
      );

      // Test invalid video upload
      const invalidFile = new File([''], 'test.txt', { type: 'text/plain' });
      act(() => {
        result.current.uploadMpushAsset(invalidFile, VIDEO.toLowerCase(), {});
      });
      expect(result.current.uploadErrors[0]).toEqual(['Only 3GP, MP4, MOV, M4V files are allowed']);
    });

    it('should handle GIF upload with validation', () => {
      const { result } = createHook();

      // Test valid GIF upload
      const validFile = new File([''], 'test.gif', { type: 'image/gif' });
      Object.defineProperty(validFile, 'size', { value: MPUSH_GIF_SIZE - 1000 }); // Just under limit
      act(() => {
        result.current.uploadMpushAsset(validFile, GIF.toLowerCase(), {});
      });
      expect(mockMobilePushActions.uploadAsset).toHaveBeenCalledWith(
        validFile,
        VIDEO.toLowerCase(), // GIF uses video type internally
        {},
        0,
        { source: MOBILE_PUSH_CHANNEL.toLowerCase(), channel: MOBILE_PUSH_CHANNEL }
      );

      // Test invalid GIF upload
      const invalidFile = new File([''], 'test.jpg', { type: 'image/jpeg' });
      act(() => {
        result.current.uploadMpushAsset(invalidFile, GIF.toLowerCase(), {});
      });
      expect(result.current.uploadErrors[0]).toEqual(['Only GIF files are allowed']);
    });
  });

  describe('Media Payload Generation', () => {
    it('should generate correct payload for IMAGE type', () => {
      const { result } = createHook();

      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image-ios.jpg',
        });
      });

      const androidPayload = result.current.getMediaPayload(IMAGE, ANDROID);
      expect(androidPayload).toHaveProperty('imageUrl');
      expect(androidPayload.imageUrl).toBe('cdn/MOBILEPUSH/test-image.jpg');

      const iosPayload = result.current.getMediaPayload(IMAGE, IOS);
      expect(iosPayload).toHaveProperty('imageUrl');
      expect(iosPayload.imageUrl).toBe('cdn/MOBILEPUSH/test-image-ios.jpg');
    });

    it('should generate correct payload for VIDEO type', () => {
      const { result } = createHook();

      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video-ios.mp4',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 30,
        });
      });

      const androidPayload = result.current.getMediaPayload(VIDEO, ANDROID);
      expect(androidPayload).toHaveProperty('videoUrl');
      expect(androidPayload).toHaveProperty('videoPreviewImg');
      expect(androidPayload).toHaveProperty('duration');
      expect(androidPayload.videoUrl).toBe('cdn/MOBILEPUSH/test-video.mp4');
      expect(androidPayload.videoPreviewImg).toBe('cdn/MOBILEPUSH/test-preview.jpg');
      expect(androidPayload.duration).toBe(30);

      const iosPayload = result.current.getMediaPayload(VIDEO, IOS);
      expect(iosPayload).toHaveProperty('videoUrl');
      expect(iosPayload).toHaveProperty('videoPreviewImg');
      expect(iosPayload).toHaveProperty('duration');
      expect(iosPayload.videoUrl).toBe('cdn/MOBILEPUSH/test-video-ios.mp4');
      expect(iosPayload.videoPreviewImg).toBe('cdn/MOBILEPUSH/test-preview-ios.jpg');
      expect(iosPayload.duration).toBe(30);
    });

    it('should generate correct payload for GIF type', () => {
      const { result } = createHook();

      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test.gif',
          androidVideoPreview: 'test-preview.jpg',
          iosVideoSrc: 'test-ios.gif',
          iosVideoPreview: 'test-preview-ios.jpg',
        });
      });

      const androidPayload = result.current.getMediaPayload(GIF, ANDROID);
      expect(androidPayload).toHaveProperty('gifUrl');
      expect(androidPayload).toHaveProperty('gifPreviewImg');
      expect(androidPayload.gifUrl).toBe('cdn/MOBILEPUSH/test.gif');
      expect(androidPayload.gifPreviewImg).toBe('cdn/MOBILEPUSH/test-preview.jpg');

      const iosPayload = result.current.getMediaPayload(GIF, IOS);
      expect(iosPayload).toHaveProperty('gifUrl');
      expect(iosPayload).toHaveProperty('gifPreviewImg');
      expect(iosPayload.gifUrl).toBe('cdn/MOBILEPUSH/test-ios.gif');
      expect(iosPayload.gifPreviewImg).toBe('cdn/MOBILEPUSH/test-preview-ios.jpg');
    });

    it('should return empty object for unknown media type', () => {
      const { result } = createHook();
      const payload = result.current.getMediaPayload('UNKNOWN', ANDROID);
      expect(payload).toEqual({});
    });
  });

  describe('Preview Data Generation', () => {
    it('should generate correct preview data for IMAGE type', () => {
      const { result } = createHook();

      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image-ios.jpg',
        });
      });

      const androidPreview = result.current.getPreviewData(IMAGE, ANDROID);
      expect(androidPreview).toHaveProperty('imageSrc', 'test-image.jpg');

      const iosPreview = result.current.getPreviewData(IMAGE, IOS);
      expect(iosPreview).toHaveProperty('imageSrc', 'test-image-ios.jpg');
    });

    it('should generate correct preview data for VIDEO type', () => {
      const { result } = createHook();

      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video-ios.mp4',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 30,
        });
      });

      const androidPreview = result.current.getPreviewData(VIDEO, ANDROID);
      expect(androidPreview).toEqual({
        videoSrc: 'test-video.mp4',
        videoPreviewImg: 'test-preview.jpg',
        duration: 30,
      });

      const iosPreview = result.current.getPreviewData(VIDEO, IOS);
      expect(iosPreview).toEqual({
        videoSrc: 'test-video-ios.mp4',
        videoPreviewImg: 'test-preview-ios.jpg',
        duration: 30,
      });
    });

    it('should generate correct preview data for GIF type', () => {
      const { result } = createHook();

      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test.gif',
          androidVideoPreview: 'test-preview.jpg',
          iosVideoSrc: 'test-ios.gif',
          iosVideoPreview: 'test-preview-ios.jpg',
        });
      });

      const androidPreview = result.current.getPreviewData(GIF, ANDROID);
      expect(androidPreview).toEqual({
        gifSrc: 'test.gif',
        gifPreviewImg: 'test-preview.jpg',
      });

      const iosPreview = result.current.getPreviewData(GIF, IOS);
      expect(iosPreview).toEqual({
        gifSrc: 'test-ios.gif',
        gifPreviewImg: 'test-preview-ios.jpg',
      });
    });

    it('should return empty object for unknown media type', () => {
      const { result } = createHook();
      const preview = result.current.getPreviewData('UNKNOWN', ANDROID);
      expect(preview).toEqual({});
    });
  });

  describe('Media Upload Status', () => {
    it('should correctly check if IMAGE is uploaded', () => {
      const { result } = createHook();

      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image-ios.jpg',
        });
      });

      expect(result.current.isMediaUploaded(IMAGE, ANDROID)).toBe(true);
      expect(result.current.isMediaUploaded(IMAGE, IOS)).toBe(true);
    });

    it('should correctly check if VIDEO is uploaded', () => {
      const { result } = createHook();

      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          iosVideoSrc: 'test-video-ios.mp4',
        });
      });

      expect(result.current.isMediaUploaded(VIDEO, ANDROID)).toBe(true);
      expect(result.current.isMediaUploaded(VIDEO, IOS)).toBe(true);
    });

    it('should correctly check if GIF is uploaded', () => {
      const { result } = createHook();

      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test.gif',
          iosVideoSrc: 'test-ios.gif',
        });
      });

      expect(result.current.isMediaUploaded(GIF, ANDROID)).toBe(true);
      expect(result.current.isMediaUploaded(GIF, IOS)).toBe(true);
    });

    it('should return true for unknown media type', () => {
      const { result } = createHook();
      expect(result.current.isMediaUploaded('UNKNOWN', ANDROID)).toBe(true);
    });
  });

  describe('Edit Mode Initialization', () => {
    it('should initialize from edit data with Android content', () => {
      const editData = {
        templateDetails: {
          versions: {
            base: {
              content: {
                ANDROID: {
                  expandableDetails: {
                    image: 'android-image.jpg',
                    video: 'android-video.mp4',
                    videoPreview: 'android-preview.jpg',
                    videoDuration: 30,
                  },
                },
              },
            },
          },
        },
      };

      const { result } = createHook({ editData });

      expect(result.current.imageSrc.androidImageSrc).toBe('android-image.jpg');
      expect(result.current.videoState.androidVideoSrc).toBe('android-video.mp4');
      expect(result.current.videoState.androidVideoPreview).toBe('android-preview.jpg');
      expect(result.current.videoState.androidVideoDuration).toBe(30);
    });

    it('should initialize from edit data with iOS content', () => {
      const editData = {
        templateDetails: {
          versions: {
            base: {
              content: {
                IOS: {
                  expandableDetails: {
                    image: 'ios-image.jpg',
                    video: 'ios-video.mp4',
                    videoPreview: 'ios-preview.jpg',
                    videoDuration: 30,
                  },
                },
              },
            },
          },
        },
      };

      const { result } = createHook({ editData });

      expect(result.current.imageSrc.iosImageSrc).toBe('ios-image.jpg');
      expect(result.current.videoState.iosVideoSrc).toBe('ios-video.mp4');
      expect(result.current.videoState.iosVideoPreview).toBe('ios-preview.jpg');
      expect(result.current.videoState.iosVideoDuration).toBe(30);
    });

    it('should handle missing video preview and duration in edit data', () => {
      const editData = {
        templateDetails: {
          versions: {
            base: {
              content: {
                ANDROID: {
                  expandableDetails: {
                    video: 'android-video.mp4',
                  },
                },
                IOS: {
                  expandableDetails: {
                    video: 'ios-video.mp4',
                  },
                },
              },
            },
          },
        },
      };

      const { result } = createHook({ editData });

      expect(result.current.videoState.androidVideoSrc).toBe('android-video.mp4');
      expect(result.current.videoState.androidVideoPreview).toBe('');
      expect(result.current.videoState.androidVideoDuration).toBe(0);
      expect(result.current.videoState.iosVideoSrc).toBe('ios-video.mp4');
      expect(result.current.videoState.iosVideoPreview).toBe('');
      expect(result.current.videoState.iosVideoDuration).toBe(0);
    });
  });

  describe('CDN URL Generation', () => {
    it('should generate CDN URLs for images and videos', () => {
      const { result } = createHook();

      // Test image URL generation
      const imageUrl = result.current.getCdnImageUrl('test-image.jpg');
      expect(imageUrl).toBeDefined();
      expect(imageUrl).toBe('cdn/MOBILEPUSH/test-image.jpg');

      // Test video URL generation
      const videoUrl = result.current.getCdnVideoUrl('test-video.mp4');
      expect(videoUrl).toBeDefined();
      expect(videoUrl).toBe('cdn/MOBILEPUSH/test-video.mp4');

      // Test empty URL handling
      expect(result.current.getCdnImageUrl('')).toBe('');
      expect(result.current.getCdnVideoUrl('')).toBe('');
    });
  });

  describe('Reset Upload States', () => {
    it('should reset all upload states', () => {
      const { result } = createHook();

      // Set some initial state
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image-ios.jpg',
        });
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video-ios.mp4',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 30,
        });
        result.current.setMpushVideoSrcAndPreview({
          mpushVideoSrc: 'test-video.mp4',
          mpushVideoPreviewImg: 'test-preview.jpg',
          duration: 30,
        });
      });

      // Reset all states
      act(() => {
        result.current.resetUploadStates();
      });

      // Verify all states are reset
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: '',
        iosImageSrc: '',
      });
      expect(result.current.videoState).toEqual({
        androidVideoSrc: '',
        iosVideoSrc: '',
        androidVideoPreview: '',
        iosVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoDuration: 0,
      });
      expect(result.current.mpushVideoSrcAndPreview).toEqual({
        mpushVideoSrc: '',
        mpushVideoPreviewImg: '',
        duration: 0,
      });
      expect(result.current.androidAssetList).toEqual([]);
      expect(result.current.iosAssetList).toEqual([]);
    });
  });

  describe('Image Re-upload Handlers', () => {
    it('should handle image re-upload with sync enabled', () => {
      const { result } = createHook({ sameContent: true });

      // Set initial state
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image.jpg',
        });
      });

      // Re-upload image
      act(() => {
        result.current.updateOnMpushImageReUpload();
      });

      // Verify both platforms are cleared
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: '',
        iosImageSrc: '',
      });
    });

    it('should handle image re-upload with sync disabled', () => {
      const { result } = createHook({ sameContent: false, activeTab: ANDROID });

      // Set initial state
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image-ios.jpg',
        });
      });

      // Re-upload image
      act(() => {
        result.current.updateOnMpushImageReUpload();
      });

      // Verify only Android is cleared
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: '',
        iosImageSrc: 'test-image-ios.jpg',
      });
    });

    it('should handle image source update with sync enabled', () => {
      const { result } = createHook({ sameContent: true });

      // Update image source
      act(() => {
        result.current.setUpdateMpushImageSrc('test-image.jpg', 0);
      });

      // Verify both platforms are updated
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: 'test-image.jpg',
        iosImageSrc: 'test-image.jpg',
      });
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);
    });

    it('should handle image source update with sync disabled', () => {
      const { result } = createHook({ sameContent: false });

      // Update Android image source
      act(() => {
        result.current.setUpdateMpushImageSrc('test-image.jpg', 0);
      });

      // Verify only Android is updated
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: 'test-image.jpg',
        iosImageSrc: '',
      });
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);

      // Update iOS image source
      act(() => {
        result.current.setUpdateMpushImageSrc('test-image-ios.jpg', 1);
      });

      // Verify iOS is updated
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: 'test-image.jpg',
        iosImageSrc: 'test-image-ios.jpg',
      });
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(1);
    });

    it('should handle carousel image source update', () => {
      const { result } = createHook({ sameContent: true });

      // Update carousel image source
      act(() => {
        result.current.setUpdateMpushImageSrc('test-image.jpg', 0, CAROUSEL);
      });

      // Verify both platforms are updated but clearAsset is not called
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: 'test-image.jpg',
        iosImageSrc: 'test-image.jpg',
      });
      expect(mockMobilePushActions.clearAsset).not.toHaveBeenCalled();
    });
  });

  describe('Video Re-upload Handlers', () => {
    it('should handle video re-upload with sync enabled', () => {
      const { result } = createHook({ sameContent: true });

      // Set initial state
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video.mp4',
          iosVideoPreview: 'test-preview.jpg',
          iosVideoDuration: 30,
        });
        result.current.setMpushVideoSrcAndPreview({
          mpushVideoSrc: 'test-video.mp4',
          mpushVideoPreviewImg: 'test-preview.jpg',
          duration: 30,
        });
      });

      // Re-upload video
      act(() => {
        result.current.updateOnMpushVideoReUpload();
      });

      // Verify all states are cleared
      expect(result.current.videoState).toEqual({
        androidVideoSrc: '',
        iosVideoSrc: '',
        androidVideoPreview: '',
        iosVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoDuration: 0,
      });
      expect(result.current.mpushVideoSrcAndPreview).toEqual({
        mpushVideoSrc: '',
        mpushVideoPreviewImg: '',
        duration: 0,
      });
      expect(result.current.androidAssetList).toEqual([]);
      expect(result.current.iosAssetList).toEqual([]);
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(1);
    });

    it('should handle video re-upload with sync disabled', () => {
      const { result } = createHook({ sameContent: false, activeTab: ANDROID });

      // Set initial state
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video-ios.mp4',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 30,
        });
      });

      // Re-upload video
      act(() => {
        result.current.updateOnMpushVideoReUpload();
      });

      // Verify only Android states are cleared
      expect(result.current.videoState).toEqual({
        androidVideoSrc: '',
        androidVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoSrc: 'test-video-ios.mp4',
        iosVideoPreview: 'test-preview-ios.jpg',
        iosVideoDuration: 30,
      });
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);
      expect(mockMobilePushActions.clearAsset).not.toHaveBeenCalledWith(1);
    });

    it('should handle video source update with sync enabled', () => {
      const { result } = createHook({ sameContent: true });

      // Update video source
      act(() => {
        result.current.setUpdateMpushVideoSrc(0, {
          videoSrc: 'test-video.mp4',
          previewUrl: 'test-preview.jpg',
          videoDuration: 30,
          videoName: 'test.mp4',
          fileHandle: 'handle',
        });
      });

      // Verify both platforms are updated
      expect(result.current.videoState).toEqual({
        androidVideoSrc: 'test-video.mp4',
        iosVideoSrc: 'test-video.mp4',
        androidVideoPreview: 'test-preview.jpg',
        iosVideoPreview: 'test-preview.jpg',
        androidVideoDuration: 30,
        iosVideoDuration: 30,
      });
      expect(result.current.mpushVideoSrcAndPreview).toEqual({
        mpushVideoSrc: 'test-video.mp4',
        mpushVideoPreviewImg: 'test-preview.jpg',
        duration: 30,
      });
      expect(result.current.androidAssetList).toEqual({
        videoSrc: 'test-video.mp4',
        previewUrl: 'test-preview.jpg',
        videoDuration: 30,
        videoName: 'test.mp4',
        fileHandle: 'handle',
      });
      expect(result.current.iosAssetList).toEqual({
        videoSrc: 'test-video.mp4',
        previewUrl: 'test-preview.jpg',
        videoDuration: 30,
        videoName: 'test.mp4',
        fileHandle: 'handle',
      });
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);
    });

    it('should handle video source update with sync disabled', () => {
      const { result } = createHook({ sameContent: false });

      // Update Android video source
      act(() => {
        result.current.setUpdateMpushVideoSrc(0, {
          videoSrc: 'test-video.mp4',
          previewUrl: 'test-preview.jpg',
          videoDuration: 30,
          videoName: 'test.mp4',
          fileHandle: 'handle',
        });
      });

      // Verify only Android is updated
      expect(result.current.videoState).toEqual({
        androidVideoSrc: 'test-video.mp4',
        androidVideoPreview: 'test-preview.jpg',
        androidVideoDuration: 30,
        iosVideoSrc: '',
        iosVideoPreview: '',
        iosVideoDuration: 0,
      });
      expect(result.current.androidAssetList).toEqual({
        videoSrc: 'test-video.mp4',
        previewUrl: 'test-preview.jpg',
        videoDuration: 30,
        videoName: 'test.mp4',
        fileHandle: 'handle',
      });
      expect(result.current.iosAssetList).toEqual([]);
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);

      // Update iOS video source
      act(() => {
        result.current.setUpdateMpushVideoSrc(1, {
          videoSrc: 'test-video-ios.mp4',
          previewUrl: 'test-preview-ios.jpg',
          videoDuration: 30,
          videoName: 'test-ios.mp4',
          fileHandle: 'handle-ios',
        });
      });

      // Verify iOS is updated
      expect(result.current.videoState).toEqual({
        androidVideoSrc: 'test-video.mp4',
        androidVideoPreview: 'test-preview.jpg',
        androidVideoDuration: 30,
        iosVideoSrc: 'test-video-ios.mp4',
        iosVideoPreview: 'test-preview-ios.jpg',
        iosVideoDuration: 30,
      });
      expect(result.current.iosAssetList).toEqual({
        videoSrc: 'test-video-ios.mp4',
        previewUrl: 'test-preview-ios.jpg',
        videoDuration: 30,
        videoName: 'test-ios.mp4',
        fileHandle: 'handle-ios',
      });
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(1);
    });

    it('should not clear asset during initialization', () => {
      const { result } = createHook({ sameContent: true });

      // Update video source with isInitialization=true
      act(() => {
        result.current.setUpdateMpushVideoSrc(0, {
          videoSrc: 'test-video.mp4',
          previewUrl: 'test-preview.jpg',
          videoDuration: 30,
          videoName: 'test.mp4',
          fileHandle: 'handle',
        }, true);
      });

      // Verify states are updated but clearAsset is not called
      expect(result.current.videoState.androidVideoSrc).toBe('test-video.mp4');
      expect(mockMobilePushActions.clearAsset).not.toHaveBeenCalled();
    });
  });

  describe('Platform-specific Logic', () => {
    it('should get correct video properties for Android', () => {
      const { result } = createHook({ activeTab: ANDROID });

      // Test Android video properties
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video-ios.mp4',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 25,
        });
      });

      const androidPayload = result.current.getMediaPayload(VIDEO, ANDROID);
      expect(androidPayload.videoUrl).toBe('cdn/MOBILEPUSH/test-video.mp4');
      expect(androidPayload.videoPreviewImg).toBe('cdn/MOBILEPUSH/test-preview.jpg');
      expect(androidPayload.duration).toBe(30);
    });

    it('should get correct video properties for iOS', () => {
      const { result } = createHook({ activeTab: IOS });

      // Test iOS video properties
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video-ios.mp4',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 25,
        });
      });

      const iosPayload = result.current.getMediaPayload(VIDEO, IOS);
      expect(iosPayload.videoUrl).toBe('cdn/MOBILEPUSH/test-video-ios.mp4');
      expect(iosPayload.videoPreviewImg).toBe('cdn/MOBILEPUSH/test-preview-ios.jpg');
      expect(iosPayload.duration).toBe(25);
    });

    it('should handle image re-upload for iOS', () => {
      const { result } = createHook({ sameContent: false, activeTab: IOS });

      // Set initial state
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image-ios.jpg',
        });
      });

      // Re-upload image
      act(() => {
        result.current.updateOnMpushImageReUpload();
      });

      // Verify only iOS is cleared
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: 'test-image.jpg',
        iosImageSrc: '',
      });
    });

    it('should handle video re-upload for iOS', () => {
      const { result } = createHook({ sameContent: false, activeTab: IOS });

      // Set initial state
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video-ios.mp4',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 25,
        });
      });

      // Re-upload video
      act(() => {
        result.current.updateOnMpushVideoReUpload();
      });

      // Verify only iOS states are cleared
      expect(result.current.videoState).toEqual({
        androidVideoSrc: 'test-video.mp4',
        androidVideoPreview: 'test-preview.jpg',
        androidVideoDuration: 30,
        iosVideoSrc: '',
        iosVideoPreview: '',
        iosVideoDuration: 0,
      });
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(1);
      expect(mockMobilePushActions.clearAsset).not.toHaveBeenCalledWith(0);
    });

    it('should handle carousel image source update for iOS', () => {
      const { result } = createHook({ sameContent: false, activeTab: IOS });

      // Update carousel image source for iOS
      act(() => {
        result.current.setUpdateMpushImageSrc('test-image-ios.jpg', 1, CAROUSEL);
      });

      // Verify only iOS is updated and clearAsset is not called
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: '',
        iosImageSrc: 'test-image-ios.jpg',
      });
      expect(mockMobilePushActions.clearAsset).not.toHaveBeenCalled();
    });

    it('should handle empty file paths', () => {
      const { result } = createHook();

      // Test empty image URL
      expect(result.current.getCdnImageUrl('')).toBe('');

      // Test empty video URL
      expect(result.current.getCdnVideoUrl('')).toBe('');

      // Test empty image source update
      act(() => {
        result.current.setUpdateMpushImageSrc('', 0);
      });
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: '',
        iosImageSrc: '',
      });

      // Test empty video source update
      act(() => {
        result.current.setUpdateMpushVideoSrc(0, {
          videoSrc: '',
          previewUrl: '',
          videoDuration: 0,
        });
      });
      expect(result.current.videoState).toEqual({
        androidVideoSrc: '',
        androidVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoSrc: '',
        iosVideoPreview: '',
        iosVideoDuration: 0,
    });
  });

    it('should handle missing video preview and duration in source update', () => {
      const { result } = createHook();

      // Update video source without preview and duration
      act(() => {
        result.current.setUpdateMpushVideoSrc(0, {
          videoSrc: 'test-video.mp4',
        });
      });

      // Verify default values are used
      expect(result.current.videoState).toEqual({
        androidVideoSrc: 'test-video.mp4',
        androidVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoSrc: '',
        iosVideoPreview: '',
        iosVideoDuration: 0,
      });
      expect(result.current.mpushVideoSrcAndPreview).toEqual({
        mpushVideoSrc: 'test-video.mp4',
        mpushVideoPreviewImg: '',
        duration: 0,
      });
    });

    it('should handle missing file name and handle in source update', () => {
      const { result } = createHook();

      const videoData = {
        videoSrc: 'test-video.mp4',
        previewUrl: 'test-preview.jpg',
        videoDuration: 30,
        videoName: '',
        fileHandle: '',
      };

      // Update video source without file name and handle
      act(() => {
        result.current.setUpdateMpushVideoSrc(0, videoData);
      });

      // Verify asset data is still updated
      expect(result.current.androidAssetList).toEqual({
        ...videoData,
      });
    });
  });

  describe('Clear Media Data', () => {
    beforeEach(() => {
      jest.clearAllMocks();
    });

    it('should clear image data with sync enabled', () => {
      const { result } = createHook({ sameContent: true });

      // Set initial state
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image.jpg',
        });
      });

      // Clear image data
      act(() => {
        result.current.clearImageDataByMediaType(IMAGE);
      });

      // Verify both platforms are cleared
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: '',
        iosImageSrc: '',
      });
    });

    it('should clear image data with sync disabled', () => {
      const { result } = createHook({ sameContent: false, activeTab: ANDROID });

      // Set initial state
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image-ios.jpg',
        });
      });

      // Clear image data
      act(() => {
        result.current.clearImageDataByMediaType(IMAGE);
      });

      // Verify only Android is cleared
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: '',
        iosImageSrc: 'test-image-ios.jpg',
      });
    });

    it('should clear video data with sync enabled', () => {
      const { result } = createHook({ sameContent: true });

      // Set initial state
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video.mp4',
          iosVideoPreview: 'test-preview.jpg',
          iosVideoDuration: 30,
        });
      });

      // Clear video data
      act(() => {
        result.current.clearImageDataByMediaType(VIDEO);
      });

      // Verify both platforms are cleared
      expect(result.current.videoState).toEqual({
        androidVideoSrc: '',
        androidVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoSrc: '',
        iosVideoPreview: '',
        iosVideoDuration: 0,
      });

      // Verify content is cleared
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
        videoSrc: '',
        videoPreview: '',
        videoDuration: 0,
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
        videoSrc: '',
        videoPreview: '',
        videoDuration: 0,
      });

      // Verify Redux state is cleared
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(1);
    });

    it('should clear video data with sync disabled', () => {
      const { result } = createHook({ sameContent: false, activeTab: ANDROID });

      // Set initial state
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video-ios.mp4',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 25,
        });
      });

      // Clear video data
      act(() => {
        result.current.clearImageDataByMediaType(VIDEO);
      });

      // Verify only Android is cleared
      expect(result.current.videoState).toEqual({
        androidVideoSrc: '',
        androidVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoSrc: 'test-video-ios.mp4',
        iosVideoPreview: 'test-preview-ios.jpg',
        iosVideoDuration: 25,
      });

      // Verify only Android content is cleared
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
        videoSrc: '',
        videoPreview: '',
        videoDuration: 0,
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
      });

      // Verify only Android Redux state is cleared
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);
      expect(mockMobilePushActions.clearAsset).not.toHaveBeenCalledWith(1);
    });

    it('should clear GIF data with sync enabled', () => {
      const { result } = createHook({ sameContent: true });

      // Set initial state
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test.gif',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test.gif',
          iosVideoPreview: 'test-preview.jpg',
          iosVideoDuration: 30,
        });
      });

      // Clear GIF data
      act(() => {
        result.current.clearImageDataByMediaType(GIF);
      });

      // Verify both platforms are cleared
      expect(result.current.videoState).toEqual({
        androidVideoSrc: '',
        androidVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoSrc: '',
        iosVideoPreview: '',
        iosVideoDuration: 0,
      });

      // Verify content is cleared
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
        videoSrc: '',
        videoPreview: '',
        videoDuration: 0,
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
        videoSrc: '',
        videoPreview: '',
        videoDuration: 0,
      });

      // Verify Redux state is cleared
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(1);
    });

    it('should clear GIF data with sync disabled', () => {
      const { result } = createHook({ sameContent: false, activeTab: ANDROID });

      // Set initial state
      act(() => {
        result.current.setVideoState({
          androidVideoSrc: 'test.gif',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-ios.gif',
          iosVideoPreview: 'test-preview-ios.jpg',
          iosVideoDuration: 25,
        });
      });

      // Clear GIF data
      act(() => {
        result.current.clearImageDataByMediaType(GIF);
      });

      // Verify only Android is cleared
      expect(result.current.videoState).toEqual({
        androidVideoSrc: '',
        androidVideoPreview: '',
        androidVideoDuration: 0,
        iosVideoSrc: 'test-ios.gif',
        iosVideoPreview: 'test-preview-ios.jpg',
        iosVideoDuration: 25,
      });

      // Verify only Android content is cleared
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
        videoSrc: '',
        videoPreview: '',
        videoDuration: 0,
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
      });

      // Verify only Android Redux state is cleared
      expect(mockMobilePushActions.clearAsset).toHaveBeenCalledWith(0);
      expect(mockMobilePushActions.clearAsset).not.toHaveBeenCalledWith(1);
    });

    it('should do nothing for unknown media type', () => {
      const { result } = createHook({ sameContent: true });

      // Set initial state
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image.jpg',
        });
        result.current.setVideoState({
          androidVideoSrc: 'test-video.mp4',
          androidVideoPreview: 'test-preview.jpg',
          androidVideoDuration: 30,
          iosVideoSrc: 'test-video.mp4',
          iosVideoPreview: 'test-preview.jpg',
          iosVideoDuration: 30,
        });
      });

      // Clear unknown media type
      act(() => {
        result.current.clearImageDataByMediaType('UNKNOWN');
      });

      // Verify nothing is cleared
      expect(result.current.imageSrc).toEqual({
        androidImageSrc: 'test-image.jpg',
        iosImageSrc: 'test-image.jpg',
      });
      expect(result.current.videoState).toEqual({
        androidVideoSrc: 'test-video.mp4',
        androidVideoPreview: 'test-preview.jpg',
        androidVideoDuration: 30,
        iosVideoSrc: 'test-video.mp4',
        iosVideoPreview: 'test-preview.jpg',
        iosVideoDuration: 30,
      });
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
      });
      expect(mockMobilePushActions.clearAsset).not.toHaveBeenCalled();
    });
  });

  describe('Image Source Updates', () => {
    it('should update content when imageSrc changes with sync enabled', () => {
      const { result } = createHook({ sameContent: true });

      // Set image source
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: '',
        });
      });

      // Verify both platforms are updated with Android image
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
        imageSrc: 'test-image.jpg',
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
        imageSrc: 'test-image.jpg',
      });

      // Set iOS image source
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: '',
          iosImageSrc: 'test-image-ios.jpg',
        });
      });

      // Verify both platforms are updated with iOS image
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
        imageSrc: 'test-image-ios.jpg',
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
        imageSrc: 'test-image-ios.jpg',
      });
    });

    it('should update content when imageSrc changes with sync disabled', () => {
      const { result } = createHook({ sameContent: false });

      // Set Android image source
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: '',
        });
      });

      // Verify only Android is updated
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
        imageSrc: 'test-image.jpg',
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
      });

      // Set iOS image source
      act(() => {
        result.current.setImageSrc({
          androidImageSrc: 'test-image.jpg',
          iosImageSrc: 'test-image-ios.jpg',
        });
      });

      // Verify both platforms have their respective images
      expect(androidContent).toMatchObject({
        title: 'Test Android',
        message: 'Test message',
        imageSrc: 'test-image.jpg',
      });
      expect(iosContent).toMatchObject({
        title: 'Test iOS',
        message: 'Test message',
        imageSrc: 'test-image-ios.jpg',
      });
    });
  });
});
