import React from 'react';
import { screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import {
    historicalContractAdvancesList,
    historicalContractAdvancesListWithNullValues,
} from 'src/__fixtures__/graphql/contract-advances';
import HistoricalContractAdvanceList from 'src/components/contract-advance-list/historical-contract-advance-list';
import HistoricalContractAdvanceListTableColumns from 'src/components/contract-advance-list/historical-contract-advance-list-table-columns';
import type { GetHistoricalContractAdvancesQuery } from 'src/apollo/queries/contract-advance/__generated__/get-historical-contract-advances';

jest.mock('react-router-dom', () => ({
    ...jest.requireActual('react-router-dom'),
    useParams: jest.fn().mockReturnValue({ contractId: 123 }),
}));

describe('<HistoricalContractAdvanceList>', () => {
    const defaultProps = {
        historicalAdvances:
            historicalContractAdvancesList as GetHistoricalContractAdvancesQuery,
        historicalAdvancesLoading: false,
        historicalAdvancesRequestParams: {
            contractId: '123',
            limit: 20,
            offset: 0,
        },
        setHistoricalAdvancesRequestParams: jest.fn(),
    };

    const render = (props: any, identity?: any) =>
        renderInAppContext(<HistoricalContractAdvanceList {...props} />, {
            identity,
        });

    it('shows message when there are no historical advances', () => {
        const updatedProps = {
            ...defaultProps,
            historicalAdvances: {
                abacusContract: {
                    contractId: '123',
                    historicalContractAdvances: { items: [], totalCount: 0 },
                },
            },
        };
        render(updatedProps);

        expect(
            screen.getByText('No historical advances found from Orchard Admin.')
        ).toBeDefined();
        expect(screen.getByTestId('EmptyStateIllustration')).toBeDefined();
    });

    it('renders a table with headers', async () => {
        render(defaultProps);
        HistoricalContractAdvanceListTableColumns().forEach((header: any) =>
            expect(screen.getByText(header.title)).toBeDefined()
        );
    });

    it('renders a list of historical advances', () => {
        render(defaultProps);
        const bodyText: any = [];
        document
            .querySelectorAll('div.GridTable-cell')
            .forEach((body: any) => bodyText.push(body.textContent));
        expect(bodyText).toEqual([
            '1,000,000.00',
            'USD (US Dollar)',
            'Recoupable Advance Fund pursuant to Section 2(b) of the April 2025 Amendment, to be paid following (i) execution, and (ii) receipt of invoice. (1/25)',
            'May 2025',
            'Apr 5, 2024',
            'Installment',
            '1.000000',
            '500,000.00',
            'GBP (Pound Sterling)',
            'Initial advance payment',
            'April 2025',
            'Mar 14, 2024',
            'Upon Execution',
            '1.270000',
        ]);
    });

    it('renders a list of historical advances with null values', () => {
        render({
            ...defaultProps,
            historicalAdvances:
                historicalContractAdvancesListWithNullValues as GetHistoricalContractAdvancesQuery,
        });
        const bodyText: any = [];
        document
            .querySelectorAll('div.GridTable-cell')
            .forEach((body: any) => bodyText.push(body.textContent));
        expect(bodyText).toEqual([
            '250,000.00',
            'EUR (Euro)',
            '-',
            '-',
            '-',
            'Delivery',
            '-',
        ]);
    });
});
