/**
 * CommonTestAndPreview Actions
 *
 * Redux action creators for CommonTestAndPreview component
 * All actions dispatch to sagas for async operations
 */

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

// ============================================
// ACTION CREATORS
// ============================================

/**
 * Search for customers by query
 * @param {string} query - Search query string
 */
export const searchCustomersRequested = (query) => ({
  type: SEARCH_CUSTOMERS_REQUESTED,
  payload: { query },
});

/**
 * Extract personalization tags from template content
 * @param {Object} payload - { templateSubject, templateContent }
 */
export const extractTagsRequested = (payload) => ({
  type: EXTRACT_TAGS_REQUESTED,
  payload, // { templateSubject, templateContent }
});

/**
 * Update preview with resolved tag values
 * @param {Object} data - Preview payload with resolved tags
 */
export const updatePreviewRequested = (data) => ({
  type: UPDATE_PREVIEW_REQUESTED,
  payload: data,
});

/**
 * Send test message to selected customers/groups
 * @param {Object} payload - Test message payload
 * @param {Function} callback - Success/error callback
 */
export const sendTestMessageRequested = (payload, callback) => ({
  type: SEND_TEST_MESSAGE_REQUESTED,
  payload,
  callback,
});

/**
 * Clear all customer search state
 */
export const clearCustomerSearchState = () => ({
  type: CLEAR_CUSTOMER_SEARCH_STATE,
});

/**
 * Clear search results only (keep other state)
 */
export const clearSearchResults = () => ({
  type: CLEAR_SEARCH_RESULTS,
});

/**
 * Fetch test customers list
 */
export const getTestCustomersRequested = () => ({
  type: GET_TEST_CUSTOMERS_REQUESTED,
});

/**
 * Fetch test groups list
 */
export const getTestGroupsRequested = () => ({
  type: GET_TEST_GROUPS_REQUESTED,
});

/**
 * Add a test customer to the list (e.g. after adding existing customer so tree shows name)
 * @param {Object} customer - { userId, customerId, name, email, mobile }
 */
export const addTestCustomer = (customer) => ({
  type: ADD_TEST_CUSTOMER,
  payload: { customer },
});

/**
 * Create or update message meta configuration
 * @param {Object} payload - Message meta payload
 * @param {string} messageMetaConfigId - Optional existing config ID for update
 * @param {Function} callback - Success/error callback
 */
export const createMessageMetaRequested = (payload, messageMetaConfigId, callback) => ({
  type: CREATE_MESSAGE_META_REQUESTED,
  payload,
  messageMetaConfigId,
  callback,
});

/**
 * Get prefilled tag values for selected customer
 * @param {Object} payload - Preview payload with customer info
 */
export const getPrefilledValuesRequested = (payload) => ({
  type: GET_PREFILLED_VALUES_REQUESTED,
  payload,
});

/**
 * Clear prefilled values from state
 */
export const clearPrefilledValues = () => ({
  type: CLEAR_PREFILLED_VALUES,
});

/**
 * Clear preview errors from state
 */
export const clearPreviewErrors = () => ({
  type: CLEAR_PREVIEW_ERRORS,
});

/**
 * Request sender details for a channel (SMS, EMAIL, WHATSAPP) for delivery settings
 * @param {Object} payload - { channel, orgUnitId }
 */
export const getSenderDetailsRequested = (payload) => ({
  type: GET_SENDER_DETAILS_REQUESTED,
  payload,
});

/**
 * Request WeCRM accounts (e.g. for WhatsApp account dropdown)
 * @param {Object} payload - { sourceName } e.g. { sourceName: 'WHATSAPP' }
 */
export const getWeCrmAccountsRequested = (payload) => ({
  type: GET_WECRM_ACCOUNTS_REQUESTED,
  payload,
});
