import React from 'react';
import { fireEvent, screen } from '@testing-library/react';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import { Adjustments } from 'src/components/adjustments/adjustments';
import { ABACUS_PROFILE, USER_FEATURES } from 'src/constants';
import * as adjustmentQuery from 'src/apollo/queries/adjustment';

describe('<Adjustments>', () => {
    let inProgressFilesRequestSpy: any;
    const mockFeatures = {
        [USER_FEATURES.ABACUS_AUTO_GENERATE_ADJUSTMENTS_FLOWTHROUGH]: false,
    };

    const mockInProgressFiles = {
        data: { abacusInProgressAutoAdjustmentFile: [] },
        loading: false,
        error: undefined,
        refetch: jest.fn().mockResolvedValue({}),
        startPolling: jest.fn(),
        stopPolling: jest.fn(),
    };
    afterEach(jest.restoreAllMocks);

    beforeEach(() => {
        inProgressFilesRequestSpy = jest
            .spyOn(adjustmentQuery, 'useInProgressAutoGenerateAdjustmentFile')
            .mockReturnValue(mockInProgressFiles);
    });
    const renderComponent = (
        features: { [x: number]: boolean } = mockFeatures
    ) =>
        renderInAppContext(<Adjustments />, {
            identity: createIdentity({
                features,
                profileType: ABACUS_PROFILE,
                id: 'Jane User',
            }),
        });

    it('displays the adjustments page title', () => {
        renderComponent();
        expect(screen.getAllByText('Adjustments')).toBeDefined();
    });

    it('renders a button to add adjustments', () => {
        renderComponent();
        const addButton = screen.getByRole('button', {
            name: '+ Add Adjustments',
        });
        expect(addButton).toBeDefined();
    });
    it('renders the import modal when the add adjustments button is clicked', () => {
        renderComponent();
        const addButton = screen.getByRole('button', {
            name: '+ Add Adjustments',
        });
        fireEvent.click(addButton);
        const adjustmentsImportModal = screen.getByText('Add Adjustments File');
        expect(adjustmentsImportModal).toBeDefined();
    });

    it('does not render "Auto-Generate Batch" button when feature flag is disabled', () => {
        renderComponent();
        expect(screen.queryByText('Auto-Generate Batch')).toBeNull();
    });

    it('renders "Auto-Generate Batch" button when feature flag is enabled', () => {
        renderComponent({
            [USER_FEATURES.ABACUS_AUTO_GENERATE_ADJUSTMENTS_FLOWTHROUGH]: true,
        });
        expect(screen.getAllByText('Auto-Generate Batch')).toBeDefined();
    });

    it('requests inProgressFiles query', () => {
        renderComponent();
        expect(inProgressFilesRequestSpy).toHaveBeenCalled();
    });

    it('starts polling if there are in-progress files', () => {
        const mockStartPolling = jest.fn();
        const mockStopPolling = jest.fn();
        jest.spyOn(
            adjustmentQuery,
            'useInProgressAutoGenerateAdjustmentFile'
        ).mockReturnValue({
            data: {
                abacusInProgressAutoAdjustmentFile: [
                    {
                        batchType: 'auto',
                        status: 'generating',
                        statementPeriodAdjustmentFile: {
                            statementPeriodAdjustmentFileId: '1',
                            fileName: 'Test File',
                        },
                    },
                ],
            },
            loading: false,
            error: undefined,
            refetch: jest.fn().mockResolvedValue({}),
            startPolling: mockStartPolling,
            stopPolling: mockStopPolling,
        });
        renderComponent({
            [USER_FEATURES.ABACUS_AUTO_GENERATE_ADJUSTMENTS_FLOWTHROUGH]: true,
        });
        expect(inProgressFilesRequestSpy).toHaveBeenCalled();
        expect(mockStartPolling).toHaveBeenCalled();
        expect(mockStopPolling).not.toHaveBeenCalled();
    });
});
