/**
 * Tests for CommonTestAndPreview Actions
 * 
 * Tests all action creators to ensure they return correct action objects
 */

import {
  searchCustomersRequested,
  extractTagsRequested,
  updatePreviewRequested,
  sendTestMessageRequested,
  clearCustomerSearchState,
  clearSearchResults,
  getTestCustomersRequested,
  getTestGroupsRequested,
  createMessageMetaRequested,
  getPrefilledValuesRequested,
  clearPrefilledValues,
  clearPreviewErrors,
  getSenderDetailsRequested,
  getWeCrmAccountsRequested,
} from '../actions';

import {
  SEARCH_CUSTOMERS_REQUESTED,
  EXTRACT_TAGS_REQUESTED,
  UPDATE_PREVIEW_REQUESTED,
  SEND_TEST_MESSAGE_REQUESTED,
  CLEAR_CUSTOMER_SEARCH_STATE,
  CLEAR_SEARCH_RESULTS,
  GET_TEST_CUSTOMERS_REQUESTED,
  GET_TEST_GROUPS_REQUESTED,
  CREATE_MESSAGE_META_REQUESTED,
  GET_PREFILLED_VALUES_REQUESTED,
  CLEAR_PREFILLED_VALUES,
  CLEAR_PREVIEW_ERRORS,
  GET_SENDER_DETAILS_REQUESTED,
  GET_WECRM_ACCOUNTS_REQUESTED,
} from '../constants';

describe('CommonTestAndPreview Actions', () => {
  describe('searchCustomersRequested', () => {
    it('should create an action to search customers with query', () => {
      const query = 'test@example.com';
      const expectedAction = {
        type: SEARCH_CUSTOMERS_REQUESTED,
        payload: { query },
      };
      expect(searchCustomersRequested(query)).toEqual(expectedAction);
    });

    it('should handle empty query string', () => {
      const query = '';
      const expectedAction = {
        type: SEARCH_CUSTOMERS_REQUESTED,
        payload: { query: '' },
      };
      expect(searchCustomersRequested(query)).toEqual(expectedAction);
    });

    it('should handle null query', () => {
      const query = null;
      const expectedAction = {
        type: SEARCH_CUSTOMERS_REQUESTED,
        payload: { query: null },
      };
      expect(searchCustomersRequested(query)).toEqual(expectedAction);
    });

    it('should handle special characters in query', () => {
      const query = 'test+user@example.com';
      const expectedAction = {
        type: SEARCH_CUSTOMERS_REQUESTED,
        payload: { query: 'test+user@example.com' },
      };
      expect(searchCustomersRequested(query)).toEqual(expectedAction);
    });
  });

  describe('extractTagsRequested', () => {
    it('should create an action to extract tags with payload', () => {
      const payload = {
        templateSubject: 'Hello {{name}}',
        templateContent: 'Welcome {{email}}',
      };
      const expectedAction = {
        type: EXTRACT_TAGS_REQUESTED,
        payload,
      };
      expect(extractTagsRequested(payload)).toEqual(expectedAction);
    });

    it('should handle empty payload', () => {
      const payload = {};
      const expectedAction = {
        type: EXTRACT_TAGS_REQUESTED,
        payload: {},
      };
      expect(extractTagsRequested(payload)).toEqual(expectedAction);
    });

    it('should handle payload with only templateSubject', () => {
      const payload = {
        templateSubject: 'Hello {{name}}',
      };
      const expectedAction = {
        type: EXTRACT_TAGS_REQUESTED,
        payload,
      };
      expect(extractTagsRequested(payload)).toEqual(expectedAction);
    });

    it('should handle payload with only templateContent', () => {
      const payload = {
        templateContent: 'Welcome {{email}}',
      };
      const expectedAction = {
        type: EXTRACT_TAGS_REQUESTED,
        payload,
      };
      expect(extractTagsRequested(payload)).toEqual(expectedAction);
    });
  });

  describe('updatePreviewRequested', () => {
    it('should create an action to update preview with data', () => {
      const data = {
        channel: 'EMAIL',
        resolvedBody: '<html>Test</html>',
        resolvedTitle: 'Test Subject',
      };
      const expectedAction = {
        type: UPDATE_PREVIEW_REQUESTED,
        payload: data,
      };
      expect(updatePreviewRequested(data)).toEqual(expectedAction);
    });

    it('should handle empty data object', () => {
      const data = {};
      const expectedAction = {
        type: UPDATE_PREVIEW_REQUESTED,
        payload: {},
      };
      expect(updatePreviewRequested(data)).toEqual(expectedAction);
    });

    it('should handle complex nested data', () => {
      const data = {
        channel: 'WHATSAPP',
        templateConfigs: {
          _id: '123',
          name: 'Test Template',
        },
        customValues: {
          'user.name': 'John',
        },
      };
      const expectedAction = {
        type: UPDATE_PREVIEW_REQUESTED,
        payload: data,
      };
      expect(updatePreviewRequested(data)).toEqual(expectedAction);
    });
  });

  describe('sendTestMessageRequested', () => {
    it('should create an action to send test message with payload and callback', () => {
      const payload = {
        channel: 'EMAIL',
        recipients: ['test@example.com'],
      };
      const callback = jest.fn();
      const expectedAction = {
        type: SEND_TEST_MESSAGE_REQUESTED,
        payload,
        callback,
      };
      expect(sendTestMessageRequested(payload, callback)).toEqual(expectedAction);
    });

    it('should handle payload without callback', () => {
      const payload = {
        channel: 'SMS',
        recipients: ['1234567890'],
      };
      const callback = undefined;
      const expectedAction = {
        type: SEND_TEST_MESSAGE_REQUESTED,
        payload,
        callback: undefined,
      };
      expect(sendTestMessageRequested(payload, callback)).toEqual(expectedAction);
    });

    it('should handle null callback', () => {
      const payload = { channel: 'EMAIL' };
      const callback = null;
      const expectedAction = {
        type: SEND_TEST_MESSAGE_REQUESTED,
        payload,
        callback: null,
      };
      expect(sendTestMessageRequested(payload, callback)).toEqual(expectedAction);
    });
  });

  describe('clearCustomerSearchState', () => {
    it('should create an action to clear customer search state', () => {
      const expectedAction = {
        type: CLEAR_CUSTOMER_SEARCH_STATE,
      };
      expect(clearCustomerSearchState()).toEqual(expectedAction);
    });
  });

  describe('clearSearchResults', () => {
    it('should create an action to clear search results', () => {
      const expectedAction = {
        type: CLEAR_SEARCH_RESULTS,
      };
      expect(clearSearchResults()).toEqual(expectedAction);
    });
  });

  describe('getTestCustomersRequested', () => {
    it('should create an action to get test customers', () => {
      const expectedAction = {
        type: GET_TEST_CUSTOMERS_REQUESTED,
      };
      expect(getTestCustomersRequested()).toEqual(expectedAction);
    });
  });

  describe('getTestGroupsRequested', () => {
    it('should create an action to get test groups', () => {
      const expectedAction = {
        type: GET_TEST_GROUPS_REQUESTED,
      };
      expect(getTestGroupsRequested()).toEqual(expectedAction);
    });
  });

  describe('createMessageMetaRequested', () => {
    it('should create an action to create message meta with all parameters', () => {
      const payload = {
        channel: 'EMAIL',
        templateId: '123',
      };
      const messageMetaConfigId = '456';
      const callback = jest.fn();
      const expectedAction = {
        type: CREATE_MESSAGE_META_REQUESTED,
        payload,
        messageMetaConfigId,
        callback,
      };
      expect(createMessageMetaRequested(payload, messageMetaConfigId, callback)).toEqual(expectedAction);
    });

    it('should handle null messageMetaConfigId', () => {
      const payload = { channel: 'SMS' };
      const messageMetaConfigId = null;
      const callback = jest.fn();
      const expectedAction = {
        type: CREATE_MESSAGE_META_REQUESTED,
        payload,
        messageMetaConfigId: null,
        callback,
      };
      expect(createMessageMetaRequested(payload, messageMetaConfigId, callback)).toEqual(expectedAction);
    });

    it('should handle undefined callback', () => {
      const payload = { channel: 'WHATSAPP' };
      const messageMetaConfigId = '789';
      const callback = undefined;
      const expectedAction = {
        type: CREATE_MESSAGE_META_REQUESTED,
        payload,
        messageMetaConfigId: '789',
        callback: undefined,
      };
      expect(createMessageMetaRequested(payload, messageMetaConfigId, callback)).toEqual(expectedAction);
    });
  });

  describe('getPrefilledValuesRequested', () => {
    it('should create an action to get prefilled values with payload', () => {
      const payload = {
        channel: 'EMAIL',
        customerId: '123',
      };
      const expectedAction = {
        type: GET_PREFILLED_VALUES_REQUESTED,
        payload,
      };
      expect(getPrefilledValuesRequested(payload)).toEqual(expectedAction);
    });

    it('should handle empty payload', () => {
      const payload = {};
      const expectedAction = {
        type: GET_PREFILLED_VALUES_REQUESTED,
        payload: {},
      };
      expect(getPrefilledValuesRequested(payload)).toEqual(expectedAction);
    });
  });

  describe('clearPrefilledValues', () => {
    it('should create an action to clear prefilled values', () => {
      const expectedAction = {
        type: CLEAR_PREFILLED_VALUES,
      };
      expect(clearPrefilledValues()).toEqual(expectedAction);
    });
  });

  describe('clearPreviewErrors', () => {
    it('should create an action to clear preview errors', () => {
      const expectedAction = {
        type: CLEAR_PREVIEW_ERRORS,
      };
      expect(clearPreviewErrors()).toEqual(expectedAction);
    });
  });

  describe('getSenderDetailsRequested', () => {
    it('should create an action to request sender details with channel and orgUnitId', () => {
      const payload = { channel: 'SMS', orgUnitId: 123 };
      const expectedAction = {
        type: GET_SENDER_DETAILS_REQUESTED,
        payload,
      };
      expect(getSenderDetailsRequested(payload)).toEqual(expectedAction);
    });

    it('should handle EMAIL channel', () => {
      const payload = { channel: 'EMAIL', orgUnitId: -1 };
      expect(getSenderDetailsRequested(payload)).toEqual({
        type: GET_SENDER_DETAILS_REQUESTED,
        payload,
      });
    });

    it('should handle WHATSAPP channel', () => {
      const payload = { channel: 'WHATSAPP', orgUnitId: 456 };
      expect(getSenderDetailsRequested(payload)).toEqual({
        type: GET_SENDER_DETAILS_REQUESTED,
        payload,
      });
    });
  });

  describe('getWeCrmAccountsRequested', () => {
    it('should create an action to request WeCRM accounts with sourceName', () => {
      const payload = { sourceName: 'WHATSAPP' };
      const expectedAction = {
        type: GET_WECRM_ACCOUNTS_REQUESTED,
        payload,
      };
      expect(getWeCrmAccountsRequested(payload)).toEqual(expectedAction);
    });

    it('should handle empty payload', () => {
      const payload = {};
      expect(getWeCrmAccountsRequested(payload)).toEqual({
        type: GET_WECRM_ACCOUNTS_REQUESTED,
        payload: {},
      });
    });
  });
});
