import { expectSaga } from 'redux-saga-test-plan';
import { call, put } from 'redux-saga/effects';
import { throwError } from 'redux-saga-test-plan/providers';
import * as Api from '../../../../services/api'; 
import * as matchers from 'redux-saga-test-plan/matchers';
import * as types from '../constants'; 
import * as sagas from '../sagas'; 
import { v2WechatMapTemplatesSagas } from '../sagas';

describe('WeChat Template Management Sagas', () => {
  describe('getDefaultWeChatTemplates Saga', () => {
    const params = { channel: 'WeChat', queryParams: { type: 'default' } };

    it.concurrent('handles fetching default WeChat templates successfully', () => {
      const fakeResponse = {
        response: {
          unMapped: [{ id: 1, name: 'Template One' }],
          mapped: [{ id: 2, name: 'Mapped Template' }]
        }
      };

      return expectSaga(sagas.getDefaultWeChatTemplates, params)
        .provide([
          [matchers.call.fn(Api.getAllTemplates, { channel: params.channel, queryParams: params.queryParams }), fakeResponse]
        ])
        .put({
          type: types.GET_WECHAT_DEFAULT_TEMPLATES_SUCCESS,
          data: fakeResponse.response.unMapped,
          templateData: fakeResponse.response.mapped
        })
        .run();
    });

    it.concurrent('handles failure in fetching default WeChat templates', () => {
      const error = new Error('Fetch failed');

      return expectSaga(sagas.getDefaultWeChatTemplates, params)
        .provide([
          [matchers.call.fn(Api.getAllTemplates, { channel: params.channel, queryParams: params.queryParams }), throwError(error)]
        ])
        .put({
          type: types.GET_WECHAT_DEFAULT_TEMPLATES_FAILURE,
          error
        })
        .run();
    });
  });

  describe('createTemplate Saga', () => {
    const template = { name: 'New WeChat Template' };
    test.concurrent('handles creating a WeChat template successfully', () => {
      const fakeResponse = {
        response: template,
        status: { code: 200 }
      };
      return expectSaga(sagas.createTemplate, template)
        .provide([
          [matchers.call.fn(Api.createWeChatTemplate, template), fakeResponse]
        ])
        .put({
          type: types.CREATE_TEMPLATE_SUCCESS,
          data: fakeResponse.response,
          statusCode: 200
        })
        .run();
    });
    test.concurrent('handles failure when creating a WeChat template', () => {
      const error = new Error('Creation failed');
      return expectSaga(sagas.createTemplate, template)
        .provide([
          [matchers.call.fn(Api.createWeChatTemplate, template), throwError(error)]
        ])
        .put({
          type: types.CREATE_TEMPLATE_FAILURE,
          error
        })
        .run();
    });
  });

  describe('fetchWeCrmAccounts Saga', () => {
    it.concurrent('handles fetching WeCrm accounts successfully', () => {
      const fakeResponse = {
        response: [{ id: 1, name: 'Account One' }]
      };

      return expectSaga(sagas.fetchWeCrmAccounts)
        .provide([
          [matchers.call.fn(Api.fetchWeCrmAccounts), fakeResponse]
        ])
        .put({
          type: types.GET_WECRM_ACCOUNTS_SUCCESS,
          data: fakeResponse.response
        })
        .run();
    });

    it.concurrent('handles failure in fetching WeCrm accounts', () => {
      const error = new Error('Fetch failed');

      return expectSaga(sagas.fetchWeCrmAccounts)
        .provide([
          [matchers.call.fn(Api.fetchWeCrmAccounts), throwError(error)]
        ])
        .put({
          type: types.GET_WECRM_ACCOUNTS_FAILURE,
          data: error
        })
        .run();
    });
  });

  describe('getTemplateDetails Saga', () => {
    const id = '123';

    it.concurrent('handles fetching template details successfully', () => {
      const fakeResponse = {
        response: { id: 123, name: 'Detailed Template' }
      };

      return expectSaga(sagas.getTemplateDetails, id)
        .provide([
          [matchers.call.fn(Api.getTemplateDetails, id), fakeResponse]
        ])
        .put({
          type: types.GET_TEMPLATE_DETAILS_SUCCESS,
          data: fakeResponse.response
        })
        .run();
    });

    it.concurrent('handles failure in fetching template details', () => {
      const error = new Error('Fetch failed');

      return expectSaga(sagas.getTemplateDetails, id)
        .provide([
          [matchers.call.fn(Api.getTemplateDetails, id), throwError(error)]
        ])
        .put({
          type: types.GET_TEMPLATE_DETAILS_FAILURE,
          error
        })
        .run();
    });
  });



  // Test combined saga
  describe('Combined v2WechatMapTemplatesSagas', () => {
    it.concurrent('should initialize all WeChat-related watcher sagas without error', () => {
      return expectSaga(v2WechatMapTemplatesSagas)
        .run();
    });
  });
});