import { fromJS } from 'immutable';
import {
  TEMPLATE_CREATE_REQUEST,
  TEMPLATE_CREATE_SUCCESS,
  TEMPLATE_CREATE_FAILURE,
  CLEAR_CREATE_RESPONSE_REQUEST,
  GET_TEMPLATE_DETAILS_REQUEST,
  GET_TEMPLATE_DETAILS_SUCCESS,
  GET_TEMPLATE_DETAILS_FAILURE,
  RESET_EDIT_TEMPLATE,
  UPLOAD_WHATSAPP_ASSET_REQUEST,
  UPLOAD_WHATSAPP_ASSET_SUCCESS,
  UPLOAD_WHATSAPP_ASSET_FAILURE,
  UPLOAD_WHATSAPP_ASSET_PROCESSING,
  UPLOAD_WHATSAPP_ASSET_COMPLETED,
  UPLOAD_WHATSAPP_ASSET_FAILED,
  UPLOAD_WHATSAPP_ASSET_TIMEOUT,
  CLEAR_WHATSAPP_ASSET,
  URL_META_TAGS_REQUEST,
  URL_META_TAGS_SUCCESS,
  URL_META_TAGS_FAILURE,
  URL_META_TAGS_RESET,
} from './constants';
import { createAsyncAssetUploadReducerCases } from '../../utils/asyncAssetUpload';

// Create generic reducer cases for async uploads
const asyncUploadCases = createAsyncAssetUploadReducerCases({
  PROCESSING: UPLOAD_WHATSAPP_ASSET_PROCESSING,
  COMPLETED: UPLOAD_WHATSAPP_ASSET_COMPLETED,
  FAILED: UPLOAD_WHATSAPP_ASSET_FAILED,
  TIMEOUT: UPLOAD_WHATSAPP_ASSET_TIMEOUT,
});

const initialState = fromJS({
  uploadedAssetData: {},
  createTemplateInProgress: false,
  getMetaTagsRequest: false,
  metaTagsDetails: {},
  getMetaTagsError: '',
  assetProcessing: {},
});

function whatsappReducer(state = initialState, action) {
  switch (action.type) {
    case TEMPLATE_CREATE_REQUEST:
      return state
        .set('createTemplateInProgress', true)
        .set('createTemplateError', false)
        .set('createTemplateErrorMessage', fromJS(''));
    case TEMPLATE_CREATE_SUCCESS:
      return state
        .set('createTemplateInProgress', false)
        .set('response', action.data)
        .set(
          'createTemplateError',
          action.statusCode !== undefined && action.statusCode > 300,
        )
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case TEMPLATE_CREATE_FAILURE:
      return state
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case CLEAR_CREATE_RESPONSE_REQUEST:
      return state.set('response', {});
    // FOR EDIT
    case GET_TEMPLATE_DETAILS_REQUEST:
      return state.set('getTemplateDetailsInProgress', true);
    case GET_TEMPLATE_DETAILS_SUCCESS:
      return state
        .set('getTemplateDetailsInProgress', false)
        .set('templateDetails', action.data);
    case GET_TEMPLATE_DETAILS_FAILURE:
      return state.set('getTemplateDetailsInProgress', false);
    case URL_META_TAGS_REQUEST:
      return state.set('getMetaTagsRequest', true)
        .set('metaTagsDetails', {});
    case URL_META_TAGS_SUCCESS:
      return state.set('getMetaTagsRequest', false)
        .set('metaTagsDetails', action?.data);
    case URL_META_TAGS_FAILURE:
      return state.set('getMetaTagsRequest', false)
      .set('getMetaTagsError', action?.error)
      .set('metaTagsDetails', {});
    case URL_META_TAGS_RESET:
      return state.set('metaTagsDetails', {});
    case RESET_EDIT_TEMPLATE:
      return state.set('templateDetails', {});
    case UPLOAD_WHATSAPP_ASSET_REQUEST:
      return state.set('uploadAssetSuccess', false).set('assetUploading', true);
    case UPLOAD_WHATSAPP_ASSET_SUCCESS:
      return state
        .set(
          'uploadAssetSuccess',
          action.statusCode !== undefined &&
            action.statusCode !== '' &&
            action.statusCode < 300,
        )
        .set('assetUploading', false)
        .set(
          action.templateType !== undefined
            ? `uploadedAssetData${action.templateType}`
            : 'uploadedAssetData',
          action.data,
        );
    case UPLOAD_WHATSAPP_ASSET_FAILURE:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);
    // P4.5: Async upload processing state management - Using generic reducer cases
    case asyncUploadCases.PROCESSING:
      return asyncUploadCases.handleProcessing(state, action);
    
    case asyncUploadCases.COMPLETED:
      return asyncUploadCases.handleCompleted(state, action);
    
    case asyncUploadCases.FAILED:
      return asyncUploadCases.handleFailed(state, action);
    
    case asyncUploadCases.TIMEOUT:
      return asyncUploadCases.handleTimeout(state, action);
    
    case CLEAR_WHATSAPP_ASSET:
      return state
        .delete(
          action.templateType !== undefined
            ? `uploadedAssetData${action.templateType}`
            : 'uploadedAssetData',
        )
        .set('assetProcessing', fromJS({})); // Clear processing state
    default:
      return state;
  }
}

export default whatsappReducer;
