import {
  call, put, takeLatest, all,
} from 'redux-saga/effects';
import * as Api from '../../services/api';
import {
  RCS,
  UPLOAD_RCS_ASSET_REQUEST,
  UPLOAD_RCS_ASSET_SUCCESS,
  UPLOAD_RCS_ASSET_FAILURE,
  CREATE_RCS_TEMPLATE_REQUEST,
  CREATE_RCS_TEMPLATE_SUCCESS,
  CREATE_RCS_TEMPLATE_FAILURE,
  CLEAR_RCS_ASSET,
  CLEAR_RCS_TEMPLATE_SUCCESS,
  CLEAR_RCS_TEMPLATE_FAILURE,
  GET_RCS_TEMPLATE_DETAILS_REQUEST,
  GET_RCS_TEMPLATE_DETAILS_SUCCESS,
  GET_RCS_TEMPLATE_DETAILS_FAILURE,
  EDIT_RCS_TEMPLATE_REQUEST,
  EDIT_RCS_TEMPLATE_SUCCESS,
  EDIT_RCS_TEMPLATE_FAILURE,
} from './constants';
export function* uploadRcsAsset({ file, assetType, fileParams, index }) {
  try {
    const result = yield call(Api.uploadFile, { file, assetType, fileParams });
    if (!result || (result.status && result.status.code >= 400)) {
      throw new Error(result?.message || 'Upload failed');
    }
    if (!result.response || !result.response.asset) {
      throw new Error('Invalid response format');
    }
    yield put({
      type: UPLOAD_RCS_ASSET_SUCCESS,
      data: result.response.asset,
      index: index ?? 0,
      statusCode: result?.status?.code || '',
    });
    return result.response.asset;
  } catch (error) {
    yield put({
      type: UPLOAD_RCS_ASSET_FAILURE, 
      error: error.message || 'Upload failed',
    });
    throw error;
  }
}

function* watchUploadRcsAsset() {
  yield takeLatest(UPLOAD_RCS_ASSET_REQUEST, uploadRcsAsset);
}

export function* createRCSTemplate({ template, callback }) {
  let errorMsg;
  try {
    const result = yield call(Api.createRcsTemplate, {
      channel: RCS,
      template,
    });
    if (result.status.code >= 400) {
      errorMsg = result.message || result.status.message || `Error: ${result.status.code}`;
      throw errorMsg;
    }
    yield put({
      type: CREATE_RCS_TEMPLATE_SUCCESS,
      data: result.response,
      statusCode: result?.status?.code || '',
      errorMsg,
    });
    if (callback) {
      callback(result.response);
    }
  } catch (error) {
    yield put({ type: CREATE_RCS_TEMPLATE_FAILURE, error, errorMsg });
    if (callback) {
      callback(null, errorMsg || error);
    }
  }
}
function* watchCreateTemplate() {
  yield takeLatest(
    CREATE_RCS_TEMPLATE_REQUEST,
    createRCSTemplate,
  );
}
// FOR EDIT
export function* editTemplate({ template, callback }) {
  let errorMsg;
  try {
    const result = yield call(Api.createRcsTemplate, {
      channel: RCS,
      template,
    });
    if (result.status.code >= 400) {
      errorMsg = result.message || result.status.message || `Error: ${result.status.code}`;
      throw errorMsg;
    }
    yield put({
      type: EDIT_RCS_TEMPLATE_SUCCESS,
      data: result.response,
      statusCode: result?.status?.code || '',
      errorMsg,
    });
    if (callback) {
      callback(result.response);
    }
  } catch (error) {
    yield put({ type: EDIT_RCS_TEMPLATE_FAILURE, error, errorMsg });
    if (callback) {
      callback(null, errorMsg || error);
    }
  }
}
function* watchEditTemplate() {
  yield takeLatest(EDIT_RCS_TEMPLATE_REQUEST, editTemplate);
}

export function* getTemplateDetails({ id, callback }) {
  try {
    const result = yield call(Api.getTemplateDetails, {
      id,
      channel: RCS,
    });
    yield put({
      type: GET_RCS_TEMPLATE_DETAILS_SUCCESS,
      data: result.response,
    });
    if (callback) {
      callback(false);
    }
  } catch (error) {
    yield put({ type: GET_RCS_TEMPLATE_DETAILS_FAILURE, error });
  }
}
function* watchGetTemplateDetails() {
  yield takeLatest(
    GET_RCS_TEMPLATE_DETAILS_REQUEST,
    getTemplateDetails,
  );
}

// All sagas to be loaded
export default [
  watchUploadRcsAsset,
  watchCreateTemplate,
  watchGetTemplateDetails,
  watchEditTemplate,
];

export function* v2RcsSagas() {
  yield all([
    watchUploadRcsAsset(),
    watchCreateTemplate(),
    watchGetTemplateDetails(),
    watchEditTemplate(),
  ]);
}
