jest.mock('../../src/interceptor/apiClient');

import apiClient from '../../src/interceptor/apiClient';
import { API_ENDPOINTS } from '@/src/utils/commonVariables';

const mockedApi = apiClient as jest.Mocked<typeof apiClient>;

describe('getMasterCountryList thunk', () => {
    const mockDispatch = jest.fn();

    /**
     * Thunk action for fetching the master country list from the API.
     * Dispatches different Redux actions based on the API response.
     * 
     * @param payload - The data required for fetching the country list, including pagination and search parameters.
     * 
     * Dispatches:
     * - REQUEST_MASTER_COUNTRY_LIST: Before the API call to indicate loading.
     * - SUCCESS_MASTER_COUNTRY_LIST: On successful API response with the country list data.
     * - ERROR_MASTER_COUNTRY_LIST: On API error.
     */
    const getMasterCountryList =
        (payload: { page: number; perPage: number }) =>
            async (dispatch: any) => {
                dispatch({ type: 'REQUEST_MASTER_COUNTRY_LIST' });
                try {
                    const response = await apiClient.post(API_ENDPOINTS.LIST_COUNTRY, payload);
                    dispatch({ type: 'SUCCESS_MASTER_COUNTRY_LIST', payload: response.data });
                    // No Toast here
                } catch (error: any) {
                    dispatch({ type: 'ERROR_MASTER_COUNTRY_LIST' });
                    // No Toast here
                }
            };

    const mockPartialListResponse = {
        data: {
            meta: { status: 1, message: 'Partial country list fetched' },
            data: Array(10).fill(null).map((_, i) => ({ countryId: `country-${i + 1}`, name: `Country ${i + 1}` })),
        },
    };

    const mockFullListResponse = {
        data: {
            meta: { status: 1, message: 'Full country list fetched' },
            data: Array(50).fill(null).map((_, i) => ({ countryId: `country-${i + 1}`, name: `Country ${i + 1}` })),
        },
    };

    beforeEach(() => {
        jest.clearAllMocks();
    });

    it('should fetch only 10 countries data', async () => {
        mockedApi.post.mockResolvedValueOnce(mockPartialListResponse);

        const payload = { page: 1, perPage: 10 };

        await getMasterCountryList(payload)(mockDispatch);

        expect(mockDispatch).toHaveBeenCalledWith({ type: 'REQUEST_MASTER_COUNTRY_LIST' });
        expect(mockedApi.post).toHaveBeenCalledWith(API_ENDPOINTS.LIST_COUNTRY, payload);
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'SUCCESS_MASTER_COUNTRY_LIST',
            payload: mockPartialListResponse.data,
        });
        expect(mockPartialListResponse.data.data).toHaveLength(10);
    });

    it('should fetch full list of countries', async () => {
        mockedApi.post.mockResolvedValueOnce(mockFullListResponse);

        const payload = { page: 1, perPage: -1 };

        await getMasterCountryList(payload)(mockDispatch);

        expect(mockDispatch).toHaveBeenCalledWith({ type: 'REQUEST_MASTER_COUNTRY_LIST' });
        expect(mockedApi.post).toHaveBeenCalledWith(API_ENDPOINTS.LIST_COUNTRY, payload);
        expect(mockDispatch).toHaveBeenCalledWith({
            type: 'SUCCESS_MASTER_COUNTRY_LIST',
            payload: mockFullListResponse.data,
        });
        expect(mockFullListResponse.data.data.length).toBeGreaterThan(10);
    });

    it('should dispatch error action on API failure', async () => {
        mockedApi.post.mockRejectedValueOnce(new Error('API failure'));

        const payload = { page: 1, perPage: 10 };

        await getMasterCountryList(payload)(mockDispatch);

        expect(mockDispatch).toHaveBeenCalledWith({ type: 'REQUEST_MASTER_COUNTRY_LIST' });
        expect(mockDispatch).toHaveBeenCalledWith({ type: 'ERROR_MASTER_COUNTRY_LIST' });
    });
});
