import { fromJS } from 'immutable';
import webPushReducer from '../reducer';
import * as types from '../constants';

describe('WebPush Reducer', () => {
  const initialState = fromJS({
    createTemplateInProgress: false,
    response: {},
    createTemplateErrorMessage: '',
    editTemplateInProgress: false,
    editResponse: {},
    editTemplateErrorMessage: '',
    uploadedAssetData: {},
    uploadAssetSuccess: false,
    assetUploading: false,
  });

  describe('Initial State', () => {
    it.concurrent('should return the initial state', () => {
      expect(webPushReducer(undefined, {})).toEqual(initialState);
    });

    it.concurrent('should return the initial state when state is undefined', () => {
      const action = { type: 'UNKNOWN_ACTION' };
      expect(webPushReducer(undefined, action)).toEqual(initialState);
    });
  });

  describe('Action Validation', () => {
    it.concurrent('should handle null action', () => {
      const currentState = initialState.set('createTemplateInProgress', true);
      expect(webPushReducer(currentState, null)).toEqual(currentState);
    });

    it.concurrent('should handle undefined action', () => {
      const currentState = initialState.set('createTemplateInProgress', true);
      expect(webPushReducer(currentState, undefined)).toEqual(currentState);
    });

    it.concurrent('should handle action without type property', () => {
      const currentState = initialState.set('createTemplateInProgress', true);
      const action = { payload: 'some data' };
      expect(webPushReducer(currentState, action)).toEqual(currentState);
    });

    it.concurrent('should handle non-object action', () => {
      const currentState = initialState.set('createTemplateInProgress', true);
      expect(webPushReducer(currentState, 'invalid')).toEqual(currentState);
    });
  });

  describe('DEFAULT_ACTION', () => {
    it.concurrent('should return the current state unchanged', () => {
      const currentState = initialState.set('createTemplateInProgress', true);
      const action = { type: types.DEFAULT_ACTION };
      expect(webPushReducer(currentState, action)).toEqual(currentState);
    });
  });

  describe('CREATE_TEMPLATE_REQUEST', () => {
    it.concurrent('should set createTemplateInProgress to true and reset error states', () => {
      const action = { type: types.CREATE_TEMPLATE_REQUEST };
      const expectedState = initialState
        .set('createTemplateInProgress', true)
        .set('createTemplateError', false)
        .set('createTemplateErrorMessage', fromJS(''));

      expect(webPushReducer(initialState, action)).toEqual(expectedState);
    });

    it.concurrent('should reset error states when request is made', () => {
      const currentState = initialState
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS('Previous error'));

      const action = { type: types.CREATE_TEMPLATE_REQUEST };
      const expectedState = currentState
        .set('createTemplateInProgress', true)
        .set('createTemplateError', false)
        .set('createTemplateErrorMessage', fromJS(''));

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });
  });

  describe('CREATE_TEMPLATE_SUCCESS', () => {
    it.concurrent('should set createTemplateInProgress to false and store response with successful status', () => {
      const mockData = { id: 1, name: 'Test Template', content: 'Test Content' };
      const action = {
        type: types.CREATE_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 200,
      };

      const currentState = initialState.set('createTemplateInProgress', true);
      const expectedState = currentState
        .set('createTemplateInProgress', false)
        .set('response', mockData)
        .set('createTemplateError', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should set createTemplateError to true when statusCode is greater than 300', () => {
      const mockData = { error: 'Template creation failed' };
      const action = {
        type: types.CREATE_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 400,
      };

      const currentState = initialState.set('createTemplateInProgress', true);
      const expectedState = currentState
        .set('createTemplateInProgress', false)
        .set('response', mockData)
        .set('createTemplateError', true);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should set createTemplateError to true when statusCode is 301', () => {
      const mockData = { id: 1, name: 'Test Template' };
      const action = {
        type: types.CREATE_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 301,
      };

      const currentState = initialState.set('createTemplateInProgress', true);
      const result = webPushReducer(currentState, action);
      
      // statusCode > 300 should set error to true
      expect(result.get('createTemplateError')).toBe(true);
    });

    it.concurrent('should handle success without statusCode', () => {
      const mockData = { id: 1, name: 'Test Template' };
      const action = {
        type: types.CREATE_TEMPLATE_SUCCESS,
        data: mockData,
      };

      const currentState = initialState.set('createTemplateInProgress', true);
      const expectedState = currentState
        .set('createTemplateInProgress', false)
        .set('response', mockData)
        .set('createTemplateError', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle success with statusCode exactly 300', () => {
      const mockData = { id: 1, name: 'Test Template' };
      const action = {
        type: types.CREATE_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 300,
      };

      const currentState = initialState.set('createTemplateInProgress', true);
      const expectedState = currentState
        .set('createTemplateInProgress', false)
        .set('response', mockData)
        .set('createTemplateError', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });
  });

  describe('CREATE_TEMPLATE_FAILURE', () => {
    it.concurrent('should set createTemplateInProgress to false and store error message', () => {
      const errorMessage = 'Template creation failed';
      const action = {
        type: types.CREATE_TEMPLATE_FAILURE,
        error: errorMessage,
      };

      const currentState = initialState.set('createTemplateInProgress', true);
      const expectedState = currentState
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(errorMessage));

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle failure with empty error message', () => {
      const action = {
        type: types.CREATE_TEMPLATE_FAILURE,
        error: '',
      };

      const currentState = initialState.set('createTemplateInProgress', true);
      const expectedState = currentState
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(''));

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle failure with undefined error', () => {
      const action = {
        type: types.CREATE_TEMPLATE_FAILURE,
        error: undefined,
      };

      const currentState = initialState.set('createTemplateInProgress', true);
      const result = webPushReducer(currentState, action);
      
      expect(result.get('createTemplateInProgress')).toBe(false);
      expect(result.get('createTemplateError')).toBe(true);
      expect(result.get('createTemplateErrorMessage')).toEqual(fromJS(undefined));
    });
  });

  describe('CLEAR_CREATE_RESPONSE_REQUEST', () => {
    it.concurrent('should clear the response', () => {
      const currentState = initialState.set('response', { id: 1, name: 'Test Template' });
      const action = { type: types.CLEAR_CREATE_RESPONSE_REQUEST };
      const expectedState = currentState.set('response', {});

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should clear response even when it is already empty', () => {
      const action = { type: types.CLEAR_CREATE_RESPONSE_REQUEST };
      const expectedState = initialState.set('response', {});

      expect(webPushReducer(initialState, action)).toEqual(expectedState);
    });
  });

  describe('EDIT_TEMPLATE_REQUEST', () => {
    it.concurrent('should set editTemplateInProgress to true and reset error states', () => {
      const action = { type: types.EDIT_TEMPLATE_REQUEST };
      const expectedState = initialState
        .set('editTemplateInProgress', true)
        .set('editTemplateError', false)
        .set('editTemplateErrorMessage', fromJS(''));

      expect(webPushReducer(initialState, action)).toEqual(expectedState);
    });

    it.concurrent('should reset error states when edit request is made', () => {
      const currentState = initialState
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', fromJS('Previous error'));

      const action = { type: types.EDIT_TEMPLATE_REQUEST };
      const expectedState = currentState
        .set('editTemplateInProgress', true)
        .set('editTemplateError', false)
        .set('editTemplateErrorMessage', fromJS(''));

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });
  });

  describe('EDIT_TEMPLATE_SUCCESS', () => {
    it.concurrent('should set editTemplateInProgress to false and store editResponse with successful status', () => {
      const mockData = { id: 1, name: 'Updated Template', content: 'Updated Content' };
      const action = {
        type: types.EDIT_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 200,
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const expectedState = currentState
        .set('editTemplateInProgress', false)
        .set('editResponse', mockData)
        .set('editTemplateError', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should set editTemplateError to true when statusCode is greater than 300', () => {
      const mockData = { error: 'Template update failed' };
      const action = {
        type: types.EDIT_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 400,
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const expectedState = currentState
        .set('editTemplateInProgress', false)
        .set('editResponse', mockData)
        .set('editTemplateError', true);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle edit success without statusCode', () => {
      const mockData = { id: 1, name: 'Updated Template' };
      const action = {
        type: types.EDIT_TEMPLATE_SUCCESS,
        data: mockData,
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const expectedState = currentState
        .set('editTemplateInProgress', false)
        .set('editResponse', mockData)
        .set('editTemplateError', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle edit success with statusCode 500', () => {
      const mockData = { error: 'Internal server error' };
      const action = {
        type: types.EDIT_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 500,
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const result = webPushReducer(currentState, action);
      
      expect(result.get('editTemplateInProgress')).toBe(false);
      expect(result.get('editResponse')).toEqual(mockData);
      expect(result.get('editTemplateError')).toBe(true);
    });

    it.concurrent('should handle edit success with statusCode exactly 300', () => {
      const mockData = { id: 1, name: 'Updated Template' };
      const action = {
        type: types.EDIT_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 300,
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const expectedState = currentState
        .set('editTemplateInProgress', false)
        .set('editResponse', mockData)
        .set('editTemplateError', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });
  });

  describe('EDIT_TEMPLATE_FAILURE', () => {
    it.concurrent('should set editTemplateInProgress to false and store error message from error property', () => {
      const errorMessage = 'Template update failed';
      const action = {
        type: types.EDIT_TEMPLATE_FAILURE,
        error: errorMessage,
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const expectedState = currentState
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', fromJS(errorMessage));

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle failure with errorMsg property when error is not present', () => {
      const errorMessage = 'Template update failed via errorMsg';
      const action = {
        type: types.EDIT_TEMPLATE_FAILURE,
        errorMsg: errorMessage,
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const expectedState = currentState
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', fromJS(errorMessage));

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should prioritize error property over errorMsg property', () => {
      const action = {
        type: types.EDIT_TEMPLATE_FAILURE,
        error: 'Primary error',
        errorMsg: 'Secondary error',
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const result = webPushReducer(currentState, action);
      
      expect(result.get('editTemplateErrorMessage')).toEqual(fromJS('Primary error'));
    });

    it.concurrent('should handle failure with empty error and errorMsg', () => {
      const action = {
        type: types.EDIT_TEMPLATE_FAILURE,
        error: '',
        errorMsg: '',
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const expectedState = currentState
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', fromJS(''));

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle failure with neither error nor errorMsg', () => {
      const action = {
        type: types.EDIT_TEMPLATE_FAILURE,
      };

      const currentState = initialState.set('editTemplateInProgress', true);
      const expectedState = currentState
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', fromJS(''));

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });
  });

  describe('CLEAR_EDIT_RESPONSE_REQUEST', () => {
    it.concurrent('should clear the editResponse', () => {
      const currentState = initialState.set('editResponse', { id: 1, name: 'Updated Template' });
      const action = { type: types.CLEAR_EDIT_RESPONSE_REQUEST };
      const expectedState = currentState.set('editResponse', {});

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should clear editResponse even when it is already empty', () => {
      const action = { type: types.CLEAR_EDIT_RESPONSE_REQUEST };
      const expectedState = initialState.set('editResponse', {});

      expect(webPushReducer(initialState, action)).toEqual(expectedState);
    });
  });

  describe('UPLOAD_WEBPUSH_ASSET_REQUEST', () => {
    it.concurrent('should set uploadAssetSuccess to false and assetUploading to true', () => {
      const action = { type: types.UPLOAD_WEBPUSH_ASSET_REQUEST };
      const expectedState = initialState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', true);

      expect(webPushReducer(initialState, action)).toEqual(expectedState);
    });

    it.concurrent('should reset uploadAssetSuccess when starting new upload', () => {
      const currentState = initialState
        .set('uploadAssetSuccess', true)
        .set('assetUploading', false);

      const action = { type: types.UPLOAD_WEBPUSH_ASSET_REQUEST };
      const expectedState = currentState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', true);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });
  });

  describe('UPLOAD_WEBPUSH_ASSET_SUCCESS', () => {
    it.concurrent('should set uploadAssetSuccess to true and store asset data with successful status', () => {
      const mockData = { url: 'https://example.com/image.jpg', id: 'asset-123' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 200,
      };

      const currentState = initialState.set('assetUploading', true);
      const expectedState = currentState
        .set('uploadAssetSuccess', true)
        .set('assetUploading', false)
        .set('uploadedAssetData', mockData);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should set uploadAssetSuccess to false when statusCode is >= 300', () => {
      const mockData = { error: 'Upload failed' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 400,
      };

      const currentState = initialState.set('assetUploading', true);
      const expectedState = currentState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false)
        .set('uploadedAssetData', mockData);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle success with statusCode 299 (success)', () => {
      const mockData = { url: 'https://example.com/image.jpg' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 299,
      };

      const currentState = initialState.set('assetUploading', true);
      const expectedState = currentState
        .set('uploadAssetSuccess', true)
        .set('assetUploading', false)
        .set('uploadedAssetData', mockData);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should set uploadAssetSuccess to false when statusCode is 300', () => {
      const mockData = { url: 'https://example.com/image.jpg' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 300,
      };

      const currentState = initialState.set('assetUploading', true);
      const expectedState = currentState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false)
        .set('uploadedAssetData', mockData);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle success without statusCode', () => {
      const mockData = { url: 'https://example.com/image.jpg' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
      };

      const currentState = initialState.set('assetUploading', true);
      const expectedState = currentState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false)
        .set('uploadedAssetData', mockData);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should set uploadAssetSuccess to false when statusCode is empty string', () => {
      const mockData = { url: 'https://example.com/image.jpg' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: '',
      };

      const currentState = initialState.set('assetUploading', true);
      const expectedState = currentState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false)
        .set('uploadedAssetData', mockData);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should store asset data with templateType when provided', () => {
      const mockData = { url: 'https://example.com/brand-icon.png' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 200,
        templateType: 'BrandIcon',
      };

      const currentState = initialState.set('assetUploading', true);
      const expectedState = currentState
        .set('uploadAssetSuccess', true)
        .set('assetUploading', false)
        .set('uploadedAssetDataBrandIcon', mockData);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should store asset data without templateType when not provided', () => {
      const mockData = { url: 'https://example.com/image.jpg' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 200,
      };

      const currentState = initialState.set('assetUploading', true);
      const expectedState = currentState
        .set('uploadAssetSuccess', true)
        .set('assetUploading', false)
        .set('uploadedAssetData', mockData);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle success with different templateType values', () => {
      const mockData = { url: 'https://example.com/media.jpg' };
      const action = {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 200,
        templateType: 'Media',
      };

      const currentState = initialState.set('assetUploading', true);
      const result = webPushReducer(currentState, action);
      
      expect(result.get('uploadAssetSuccess')).toBe(true);
      expect(result.get('assetUploading')).toBe(false);
      expect(result.get('uploadedAssetDataMedia')).toEqual(mockData);
    });
  });

  describe('UPLOAD_WEBPUSH_ASSET_FAILURE', () => {
    it.concurrent('should set uploadAssetSuccess to false and assetUploading to false', () => {
      const action = { type: types.UPLOAD_WEBPUSH_ASSET_FAILURE };
      const currentState = initialState
        .set('uploadAssetSuccess', true)
        .set('assetUploading', true);

      const expectedState = currentState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle failure when upload was in progress', () => {
      const action = { type: types.UPLOAD_WEBPUSH_ASSET_FAILURE };
      const currentState = initialState.set('assetUploading', true);

      const expectedState = currentState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });

    it.concurrent('should handle failure when upload was already successful', () => {
      const action = { type: types.UPLOAD_WEBPUSH_ASSET_FAILURE };
      const currentState = initialState
        .set('uploadAssetSuccess', true)
        .set('assetUploading', false)
        .set('uploadedAssetData', { url: 'https://example.com/image.jpg' });

      const expectedState = currentState
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);

      expect(webPushReducer(currentState, action)).toEqual(expectedState);
    });
  });

  describe('CLEAR_WEBPUSH_ASSET', () => {
    it.concurrent('should delete uploadedAssetData when templateType is not provided', () => {
      const currentState = initialState.set('uploadedAssetData', { url: 'https://example.com/image.jpg' });
      const action = { type: types.CLEAR_WEBPUSH_ASSET };
      const result = webPushReducer(currentState, action);

      expect(result.has('uploadedAssetData')).toBe(false);
    });

    it.concurrent('should delete uploadedAssetData with templateType when provided', () => {
      const currentState = initialState.set('uploadedAssetDataBrandIcon', { url: 'https://example.com/brand-icon.png' });
      const action = {
        type: types.CLEAR_WEBPUSH_ASSET,
        templateType: 'BrandIcon',
      };
      const result = webPushReducer(currentState, action);

      expect(result.has('uploadedAssetDataBrandIcon')).toBe(false);
    });

    it.concurrent('should handle clear when asset data does not exist', () => {
      const action = { type: types.CLEAR_WEBPUSH_ASSET };
      const result = webPushReducer(initialState, action);

      expect(result.has('uploadedAssetData')).toBe(false);
    });

    it.concurrent('should handle clear with different templateType values', () => {
      const currentState = initialState.set('uploadedAssetDataMedia', { url: 'https://example.com/media.jpg' });
      const action = {
        type: types.CLEAR_WEBPUSH_ASSET,
        templateType: 'Media',
      };
      const result = webPushReducer(currentState, action);

      expect(result.has('uploadedAssetDataMedia')).toBe(false);
    });

    it.concurrent('should only delete the specified templateType asset, not others', () => {
      const currentState = initialState
        .set('uploadedAssetData', { url: 'https://example.com/default.jpg' })
        .set('uploadedAssetDataBrandIcon', { url: 'https://example.com/brand-icon.png' });

      const action = {
        type: types.CLEAR_WEBPUSH_ASSET,
        templateType: 'BrandIcon',
      };
      const result = webPushReducer(currentState, action);

      expect(result.has('uploadedAssetDataBrandIcon')).toBe(false);
      expect(result.has('uploadedAssetData')).toBe(true);
    });
  });

  describe('Default Case', () => {
    it.concurrent('should return current state for unknown action types', () => {
      const currentState = initialState.set('createTemplateInProgress', true);
      const action = { type: 'UNKNOWN_ACTION_TYPE' };
      expect(webPushReducer(currentState, action)).toEqual(currentState);
    });
  });

  describe('Integration Scenarios', () => {
    it.concurrent('should handle complete create template flow', () => {
      let state = initialState;

      // Request
      state = webPushReducer(state, { type: types.CREATE_TEMPLATE_REQUEST });
      expect(state.get('createTemplateInProgress')).toBe(true);
      expect(state.get('createTemplateError')).toBe(false);

      // Success
      const mockData = { id: 1, name: 'Test Template' };
      state = webPushReducer(state, {
        type: types.CREATE_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 200,
      });
      expect(state.get('createTemplateInProgress')).toBe(false);
      expect(state.get('response')).toEqual(mockData);
      expect(state.get('createTemplateError')).toBe(false);

      // Clear
      state = webPushReducer(state, { type: types.CLEAR_CREATE_RESPONSE_REQUEST });
      expect(state.get('response')).toEqual({});
    });

    it.concurrent('should handle create template failure flow', () => {
      let state = initialState;

      // Request
      state = webPushReducer(state, { type: types.CREATE_TEMPLATE_REQUEST });
      expect(state.get('createTemplateInProgress')).toBe(true);

      // Failure
      state = webPushReducer(state, {
        type: types.CREATE_TEMPLATE_FAILURE,
        error: 'Creation failed',
      });
      expect(state.get('createTemplateInProgress')).toBe(false);
      expect(state.get('createTemplateError')).toBe(true);
      expect(state.get('createTemplateErrorMessage')).toEqual(fromJS('Creation failed'));
    });

    it.concurrent('should handle complete edit template flow', () => {
      let state = initialState;

      // Request
      state = webPushReducer(state, { type: types.EDIT_TEMPLATE_REQUEST });
      expect(state.get('editTemplateInProgress')).toBe(true);

      // Success
      const mockData = { id: 1, name: 'Updated Template' };
      state = webPushReducer(state, {
        type: types.EDIT_TEMPLATE_SUCCESS,
        data: mockData,
        statusCode: 200,
      });
      expect(state.get('editTemplateInProgress')).toBe(false);
      expect(state.get('editResponse')).toEqual(mockData);

      // Clear
      state = webPushReducer(state, { type: types.CLEAR_EDIT_RESPONSE_REQUEST });
      expect(state.get('editResponse')).toEqual({});
    });

    it.concurrent('should handle edit template failure flow', () => {
      let state = initialState;

      // Request
      state = webPushReducer(state, { type: types.EDIT_TEMPLATE_REQUEST });
      expect(state.get('editTemplateInProgress')).toBe(true);

      // Failure
      state = webPushReducer(state, {
        type: types.EDIT_TEMPLATE_FAILURE,
        error: 'Update failed',
      });
      expect(state.get('editTemplateInProgress')).toBe(false);
      expect(state.get('editTemplateError')).toBe(true);
      expect(state.get('editTemplateErrorMessage')).toEqual(fromJS('Update failed'));
    });

    it.concurrent('should handle complete asset upload flow', () => {
      let state = initialState;

      // Request
      state = webPushReducer(state, { type: types.UPLOAD_WEBPUSH_ASSET_REQUEST });
      expect(state.get('assetUploading')).toBe(true);
      expect(state.get('uploadAssetSuccess')).toBe(false);

      // Success
      const mockData = { url: 'https://example.com/image.jpg' };
      state = webPushReducer(state, {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 200,
      });
      expect(state.get('assetUploading')).toBe(false);
      expect(state.get('uploadAssetSuccess')).toBe(true);
      expect(state.get('uploadedAssetData')).toEqual(mockData);

      // Clear
      state = webPushReducer(state, { type: types.CLEAR_WEBPUSH_ASSET });
      expect(state.has('uploadedAssetData')).toBe(false);
    });

    it.concurrent('should handle asset upload failure flow', () => {
      let state = initialState;

      // Request
      state = webPushReducer(state, { type: types.UPLOAD_WEBPUSH_ASSET_REQUEST });
      expect(state.get('assetUploading')).toBe(true);

      // Failure
      state = webPushReducer(state, { type: types.UPLOAD_WEBPUSH_ASSET_FAILURE });
      expect(state.get('assetUploading')).toBe(false);
      expect(state.get('uploadAssetSuccess')).toBe(false);
    });

    it.concurrent('should handle asset upload with templateType flow', () => {
      let state = initialState;

      // Request
      state = webPushReducer(state, { type: types.UPLOAD_WEBPUSH_ASSET_REQUEST });

      // Success with templateType
      const mockData = { url: 'https://example.com/brand-icon.png' };
      state = webPushReducer(state, {
        type: types.UPLOAD_WEBPUSH_ASSET_SUCCESS,
        data: mockData,
        statusCode: 200,
        templateType: 'BrandIcon',
      });
      expect(state.get('uploadedAssetDataBrandIcon')).toEqual(mockData);

      // Clear with templateType
      state = webPushReducer(state, {
        type: types.CLEAR_WEBPUSH_ASSET,
        templateType: 'BrandIcon',
      });
      expect(state.has('uploadedAssetDataBrandIcon')).toBe(false);
    });
  });
});

