/**
 *
 * TestAndPreviewSlidebox - Unified Wrapper for All Channels
 *
 * This is now a thin wrapper around CommonTestAndPreview component
 * Supports all channels: EMAIL, SMS, RCS, WHATSAPP, INAPP, MOBILEPUSH
 * Maintains backward compatibility with existing containers
 */

import React from 'react';
import PropTypes from 'prop-types';
import { compose, bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { injectIntl } from 'react-intl';
import { DAEMON } from '@capillarytech/vulcan-react-sdk/utils/sagaInjectorTypes';
import injectReducer from '../../utils/injectReducer';
import injectSaga from '../../utils/injectSaga';

import CommonTestAndPreview from '../CommonTestAndPreview';
import { CHANNELS } from '../CommonTestAndPreview/constants';
import * as commonTestAndPreviewActions from '../CommonTestAndPreview/actions';
import { commonTestAndPreviewSaga } from '../CommonTestAndPreview/sagas';
import commonTestAndPreviewReducer from '../CommonTestAndPreview/reducer';
import {
  makeSelectExtractedTags,
  makeSelectIsExtractingTags,
  makeSelectCustomers,
  makeSelectIsSearchingCustomer,
  makeSelectPreviewData,
  makeSelectIsUpdatingPreview,
  makeSelectTestCustomers,
  makeSelectIsFetchingTestCustomers,
  makeSelectTestGroups,
  makeSelectIsFetchingTestGroups,
  makeSelectMessageMetaConfigId,
  makeSelectPrefilledValues,
  makeSelectIsSendingTestMessage,
  makeSelectUpdatePreviewError,
  makeSelectUpdatePreviewErrors,
  makeSelectFetchPrefilledValuesError,
  makeSelectFetchPrefilledValuesErrors,
  makeSelectSenderDetailsByChannel,
  makeSelectWeCrmAccounts,
  makeSelectIsLoadingSenderDetails,
} from '../CommonTestAndPreview/selectors';

/**
 * Wrapper component that maintains the old API while using the new CommonTestAndPreview
 * Supports all channels via channel prop (defaults to EMAIL for backward compatibility)
 */
const TestAndPreviewSlidebox = (props) => {
  const { channel, currentChannel, ...restProps } = props;
  // Use channel prop, or currentChannel prop, or default to EMAIL for backward compatibility
  const selectedChannel = channel || currentChannel || CHANNELS.EMAIL;
  return (
    <CommonTestAndPreview
      {...restProps}
      channel={selectedChannel}
      config={{
        enableCustomerSearch: true,
        enableTagExtraction: true,
        enableTestMessage: true,
        enableDeviceToggle: true,
      }}
    />
  );
};

TestAndPreviewSlidebox.propTypes = {
  // Channel prop - supports all channels
  channel: PropTypes.oneOf([CHANNELS.EMAIL, CHANNELS.SMS, CHANNELS.RCS, CHANNELS.WHATSAPP, CHANNELS.INAPP, CHANNELS.MOBILEPUSH, CHANNELS.VIBER, CHANNELS.ZALO, CHANNELS.WEBPUSH]),
  currentChannel: PropTypes.oneOf([CHANNELS.EMAIL, CHANNELS.SMS, CHANNELS.RCS, CHANNELS.WHATSAPP, CHANNELS.INAPP, CHANNELS.MOBILEPUSH, CHANNELS.VIBER, CHANNELS.ZALO, CHANNELS.WEBPUSH]), // Alternative prop name for backward compatibility
  // All original props are passed through
  show: PropTypes.bool.isRequired,
  onClose: PropTypes.func.isRequired,
  formData: PropTypes.object,
  content: PropTypes.string,
  beeInstance: PropTypes.object,
  currentTab: PropTypes.number,
  // Redux props are passed through
  actions: PropTypes.object.isRequired,
  extractedTags: PropTypes.array.isRequired,
  isExtractingTags: PropTypes.bool.isRequired,
  customers: PropTypes.array.isRequired,
  isSearchingCustomer: PropTypes.bool.isRequired,
  previewData: PropTypes.object.isRequired,
  isUpdatingPreview: PropTypes.bool.isRequired,
  testCustomers: PropTypes.array.isRequired,
  isFetchingTestCustomers: PropTypes.bool.isRequired,
  testGroups: PropTypes.array.isRequired,
  isFetchingTestGroups: PropTypes.bool.isRequired,
  messageMetaConfigId: PropTypes.string,
  prefilledValues: PropTypes.object,
  isSendingTestMessage: PropTypes.bool.isRequired,
  intl: PropTypes.object.isRequired,
  senderDetailsByChannel: PropTypes.object,
  wecrmAccounts: PropTypes.array,
  isLoadingSenderDetails: PropTypes.bool,
  orgUnitId: PropTypes.number,
};

TestAndPreviewSlidebox.defaultProps = {
  channel: null,
  currentChannel: null,
  formData: null,
  content: '',
  beeInstance: null,
  currentTab: 1,
  messageMetaConfigId: null,
  prefilledValues: {},
  senderDetailsByChannel: {},
  wecrmAccounts: [],
  isLoadingSenderDetails: false,
  orgUnitId: -1,
};

const mapStateToProps = createStructuredSelector({
  extractedTags: makeSelectExtractedTags(),
  isExtractingTags: makeSelectIsExtractingTags(),
  customers: makeSelectCustomers(),
  isSearchingCustomer: makeSelectIsSearchingCustomer(),
  previewData: makeSelectPreviewData(),
  isUpdatingPreview: makeSelectIsUpdatingPreview(),
  updatePreviewError: makeSelectUpdatePreviewError(),
  updatePreviewErrors: makeSelectUpdatePreviewErrors(),
  testCustomers: makeSelectTestCustomers(),
  isFetchingTestCustomers: makeSelectIsFetchingTestCustomers(),
  testGroups: makeSelectTestGroups(),
  isFetchingTestGroups: makeSelectIsFetchingTestGroups(),
  messageMetaConfigId: makeSelectMessageMetaConfigId(),
  prefilledValues: makeSelectPrefilledValues(),
  fetchPrefilledValuesError: makeSelectFetchPrefilledValuesError(),
  fetchPrefilledValuesErrors: makeSelectFetchPrefilledValuesErrors(),
  isSendingTestMessage: makeSelectIsSendingTestMessage(),
  senderDetailsByChannel: makeSelectSenderDetailsByChannel(),
  wecrmAccounts: makeSelectWeCrmAccounts(),
  isLoadingSenderDetails: makeSelectIsLoadingSenderDetails(),
});

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(commonTestAndPreviewActions, dispatch),
  };
}

const withReducer = injectReducer({ key: 'commonTestAndPreview', reducer: commonTestAndPreviewReducer });
const withSaga = injectSaga({ key: 'commonTestAndPreview', saga: commonTestAndPreviewSaga, mode: DAEMON });
const withConnect = connect(mapStateToProps, mapDispatchToProps);

export default compose(
  withReducer,
  withSaga,
  withConnect,
  injectIntl,
)(TestAndPreviewSlidebox);
