import { expectSaga } from 'redux-saga-test-plan';
import * as matchers from 'redux-saga-test-plan/matchers';
import { throwError } from 'redux-saga-test-plan/providers';
import * as Api from '../../../services/api';
import * as sagas from '../sagas';
import * as types from '../constants';
describe('getBulkCustomerDetails Saga', () => {
  it('handles success with data transformation', () => {
    expectSaga(sagas.getBulkCustomerDetails, { fetchedUserIds: [1], callback: jest.fn() })
      .provide([
        [matchers.call.fn(Api.getBulkCustomerDetails), {
          result: {
            data: [
              {
                entity: {
                  id: 1,
                  profiles: [{
                    firstName: 'John',
                    lastName: 'Doe',
                    identifiers: [{
                      type: 'mobile',
                      value: '1234567890',
                    }, {
                      type: 'email',
                      value: 'johndoe@example.com',
                    }],
                  }],
                },
              },
            ],
          },
        }],
      ])
      .put({
        type: types.FETCH_TEST_CUSTOMERS_SUCCESS,
        data: [
          {
            userId: 1,
            name: 'John Doe',
            mobile: '1234567890',
            email: 'johndoe@example.com',
          },
        ],
      })
      .run();
  });

  it('handles empty identifiers gracefully', () => {
    expectSaga(sagas.getBulkCustomerDetails, { fetchedUserIds: [1], callback: jest.fn() })
      .provide([
        [matchers.call.fn(Api.getBulkCustomerDetails), {
          result: {
            data: [
              {
                entity: {
                  id: 1,
                  profiles: [{
                    firstName: 'John',
                    lastName: 'Doe',
                    identifiers: [],
                  }],
                },
              },
            ],
          },
        }],
      ])
      .put({
        type: types.FETCH_TEST_CUSTOMERS_SUCCESS,
        data: [
          {
            userId: 1,
            name: 'John Doe',
            mobile: undefined,
            email: undefined,
          },
        ],
      })
      .run();
  });

  it('handles API failure', () => {
    const error = new Error('API error');
    expectSaga(sagas.getBulkCustomerDetails, { fetchedUserIds: [1] })
      .provide([
        [matchers.call.fn(Api.getBulkCustomerDetails), throwError(error)],
      ])
      .put({
        type: types.FETCH_TEST_CUSTOMERS_FAILURE,
        error,
      })
      .run();
  });

  it('calls callback with transformed data on success', () => {
    const callback = jest.fn();
    expectSaga(sagas.getBulkCustomerDetails, { fetchedUserIds: [1], callback })
      .provide([
        [matchers.call.fn(Api.getBulkCustomerDetails), {
          result: {
            data: [
              {
                entity: {
                  id: 1,
                  profiles: [{
                    firstName: 'John',
                    lastName: 'Doe',
                    identifiers: [{
                      type: 'mobile',
                      value: '1234567890',
                    }, {
                      type: 'email',
                      value: 'johndoe@example.com',
                    }],
                  }],
                },
              },
            ],
          },
        }],
      ])
      .run()
      .then(() => {
        expect(callback).toHaveBeenCalledWith([
          {
            userId: 1,
            name: 'John Doe',
            mobile: '1234567890',
            email: 'johndoe@example.com',
          },
        ]);
      });
  });

  it('completes successfully with no data', () => {
    expectSaga(sagas.getBulkCustomerDetails, { fetchedUserIds: [], callback: jest.fn() })
      .provide([
        [matchers.call.fn(Api.getBulkCustomerDetails), {
          result: { data: [] },
        }],
      ])
      .run();
  });

  it('handles group flow with transformed data', () => {
    const groupId = '123';
    const groupName = 'Test Group';
    expectSaga(sagas.getBulkCustomerDetails, {
      fetchedUserIds: [1],
      callback: jest.fn(),
      isGroupFlow: true,
      groupId,
      groupName,
    })
      .provide([
        [matchers.call.fn(Api.getBulkCustomerDetails), {
          result: {
            data: [
              {
                entity: {
                  id: 1,
                  profiles: [{
                    firstName: 'John',
                    lastName: 'Doe',
                    identifiers: [{
                      type: 'mobile',
                      value: '1234567890',
                    }, {
                      type: 'email',
                      value: 'johndoe@example.com',
                    }],
                  }],
                },
              },
            ],
          },
        }],
      ])
      .put({
        type: types.FETCH_TEST_CUSTOMERS_SUCCESS,
        data: [
          {
            userId: 1,
            name: 'John Doe',
            mobile: '1234567890',
            email: 'johndoe@example.com',
          },
        ],
      })
      .run();
  });
});
