import React from 'react';
import { screen } from '@testing-library/react';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import { accountDetail } from 'src/__fixtures__/graphql/account-detail';
import { PaymentType } from 'src/apollo/definitions/globalTypes';
import { useAccountPayeeTaxFormInfoESQuery } from 'src/apollo/queries/account-payee-tax-form-info-es';
import { useCanPerformAction } from 'src/apollo/queries/can-perform';
import { COUNTRY_NAMES } from '../components/taxFormInfoCompleted/constants';
import TaxFormInfo, { TaxFormInfoPropTypes } from '../tax-form-info';
import {
    ABACUS_ACTION_STATUSES,
    ABACUS_ACTIONS,
    PAYMENT_ENTITY_NAMES,
} from '@theorchard/accounting-apps-shared';
import { USA_ISO_COUNTRY_CODE, USER_FEATURES } from 'src/constants';
import { Identity } from '@theorchard/suite-frontend';
import type { GetAccountFullDetailQuery } from 'src/apollo/queries/account/__generated__/get-account-full-detail';

jest.mock('src/apollo/queries/account-payee-tax-form-info-es', () => ({
    useAccountPayeeTaxFormInfoESQuery: jest.fn(),
}));

jest.mock('src/apollo/queries/can-perform', () => ({
    useCanPerformAction: jest.fn(),
}));

describe('<TaxFormInfo>', () => {
    const defaultData = accountDetail;

    afterEach(jest.restoreAllMocks);

    const render = (
        props?: Partial<TaxFormInfoPropTypes>,
        identity?: Identity
    ) => {
        (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
            data: undefined,
            loading: false,
        });

        (useCanPerformAction as jest.Mock).mockReturnValue({
            data: true,
            loading: false,
        });

        return renderInAppContext(
            <TaxFormInfo
                accountFullDetailQueryLoading={false}
                accountFullDetail={defaultData}
                {...props}
            />,
            {
                identity,
            }
        );
    };

    it('renders', () => {
        const { container } = render();
        expect(container).toBeDefined();
        expect(screen.getByTestId('TaxFormInfo')).toBeInTheDocument();
    });

    it('shows error alert if accountId is missing', () => {
        const modifiedData = {
            ...accountDetail,
            abacusAccount: null,
        };

        (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
            data: undefined,
            loading: false,
        });

        renderInAppContext(
            <TaxFormInfo
                accountFullDetailQueryLoading={false}
                accountFullDetail={modifiedData}
            />
        );
        expect(screen.getByText('Account not found')).toBeInTheDocument();
    });

    it('renders Loading state', () => {
        (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
            data: undefined,
            loading: true,
        });

        renderInAppContext(
            <TaxFormInfo
                accountFullDetailQueryLoading={true}
                accountFullDetail={defaultData}
            />
        );
        expect(screen.getByTestId('SkeletonLoader')).toBeInTheDocument();
    });

    it('passes canEditUSTaxInfo=true to children when permission is granted and paymentEntity is ORCHARD-US', () => {
        (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
            data: undefined,
            loading: false,
        });

        (useCanPerformAction as jest.Mock).mockReturnValue({
            data: true,
            loading: false,
        });

        const modifiedData = {
            ...accountDetail,
            abacusAccount: {
                ...accountDetail.abacusAccount,
                accountPaymentTerm: {
                    ...accountDetail.abacusAccount.accountPaymentTerm,
                    paymentEntity: {
                        paymentEntityName: PAYMENT_ENTITY_NAMES.ORCHARD_US,
                        referencePaymentEntityId: '123',
                    },
                },
            },
        };

        renderInAppContext(
            <TaxFormInfo
                accountFullDetailQueryLoading={false}
                accountFullDetail={modifiedData}
            />
        );

        expect(screen.getByTestId('TaxFormInfoTopSection')).toBeInTheDocument();
    });

    it('renders PAYONEER NOT WHITELEBEL content', () => {
        render();
        expect(screen.getByTestId('accountTaxInfo')).toBeInTheDocument();
    });

    describe('PAYONEER_WHITELABEL view logic', () => {
        const renderWithStatus = (status: string, expirationDate?: string) => {
            const modifiedData = {
                ...accountDetail,
                abacusAccount: {
                    ...accountDetail.abacusAccount,
                    accountPaymentTerm: {
                        ...accountDetail.abacusAccount.accountPaymentTerm,
                        paymentType: PaymentType.PAYONEER_WHITELABEL,
                        // Ensure paymentEntityName is not ORCHARD-ES for these tests
                        paymentEntity: {
                            ...accountDetail.abacusAccount.accountPaymentTerm
                                .paymentEntity,
                            paymentEntityName: 'ORCHARD-US',
                            countryOfTaxReporting: USA_ISO_COUNTRY_CODE,
                        },
                    },
                    accountPayee: {
                        ...accountDetail.abacusAccount.accountPayee,
                        referencePaymentType: {
                            paymentService: 'payoneer_whitelabel',
                            referencePaymentTypeId: '8',
                        },
                        actionStates: [
                            {
                                abacusStateId: '36546',
                                actionName: ABACUS_ACTIONS.TAX_ELIGIBILITY,
                                actionStatus: status,
                                createdAt: '',
                                lastModified: '',
                                message: '',
                            },
                        ],
                        accountPayeeTaxFormInfo: expirationDate
                            ? { expirationDate }
                            : null,
                    },
                },
            };
            (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
                data: undefined,
                loading: false,
            });
            (useCanPerformAction as jest.Mock).mockReturnValue({
                data: true,
                loading: false,
            });

            renderInAppContext(
                <TaxFormInfo
                    accountFullDetailQueryLoading={false}
                    accountFullDetail={modifiedData}
                />
            );
        };

        it('shows PENDING view for INIT status', () => {
            renderWithStatus(ABACUS_ACTION_STATUSES.INIT);
            expect(
                screen.getByTestId('TaxFormInfoTopSection')
            ).toBeInTheDocument();
            expect(
                screen.getByText('Tax information has not been added yet')
            ).toBeInTheDocument();
            expect(
                screen.getByText('Add Tax Form details')
            ).toBeInTheDocument();
            expect(
                screen.queryByTestId('TaxFormInfoCompleted')
            ).not.toBeInTheDocument();
            expect(
                screen.queryByTestId('accountTaxInfo')
            ).not.toBeInTheDocument();
        });

        it('shows COMPLETED view and <TaxFormInfoCompleted /> for COMPLETE status', () => {
            renderWithStatus(ABACUS_ACTION_STATUSES.COMPLETE);
            expect(
                screen.getByTestId('TaxFormInfoTopSection')
            ).toBeInTheDocument();
            expect(screen.getByTestId('CheckGlyphIcon')).toBeInTheDocument();
        });

        it('shows REJECTED view', () => {
            renderWithStatus(ABACUS_ACTION_STATUSES.REJECTED);
            expect(
                screen.getByTestId('TaxFormInfoTopSection')
            ).toBeInTheDocument();
            expect(
                screen.queryByTestId('CheckGlyphIcon')
            ).not.toBeInTheDocument();
            expect(screen.getByText('Tax form rejected')).toBeInTheDocument();
            expect(
                screen.getByText(
                    'Please request an updated tax form from the client.'
                )
            ).toBeInTheDocument();
            expect(
                screen.getByText('I’ve received a new tax form')
            ).toBeInTheDocument();
            expect(
                screen.queryByTestId('TaxFormInfoCompleted')
            ).not.toBeInTheDocument();
            expect(
                screen.queryByTestId('accountTaxInfo')
            ).not.toBeInTheDocument();
        });

        it('shows EXPIRED view for REJECTED status with expired date', () => {
            const expiredDate = '06/03/2024';
            renderWithStatus(ABACUS_ACTION_STATUSES.REJECTED, expiredDate);
            expect(
                screen.getByTestId('TaxFormInfoTopSection')
            ).toBeInTheDocument();
            expect(
                screen.getByText(`Tax form expired on ${expiredDate}`)
            ).toBeInTheDocument();
            expect(
                screen.getByText(
                    'Please request an updated tax form from the client.'
                )
            ).toBeInTheDocument();
            expect(
                screen.getByText('I’ve received a new tax form')
            ).toBeInTheDocument();
            expect(
                screen.queryByTestId('TaxFormInfoCompleted')
            ).not.toBeInTheDocument();
            expect(
                screen.queryByTestId('accountTaxInfo')
            ).not.toBeInTheDocument();
        });

        it('shows PENDING view for RUNNING status', () => {
            renderWithStatus(ABACUS_ACTION_STATUSES.RUNNING);
            expect(
                screen.getByTestId('TaxFormInfoTopSection')
            ).toBeInTheDocument();
            expect(
                screen.getByText('Add Tax Form details')
            ).toBeInTheDocument();
            expect(
                screen.getByText('Tax information has not been added yet')
            ).toBeInTheDocument();
            expect(
                screen.queryByTestId('TaxFormInfoCompleted')
            ).not.toBeInTheDocument();
            expect(
                screen.queryByTestId('accountTaxInfo')
            ).not.toBeInTheDocument();
        });

        describe('USA (AWAL) Migration Logic', () => {
            const getAccountData = ({
                entityName,
                countryCode,
                paymentType = PaymentType.PAYONEER_WHITELABEL,
                paymentService = 'payoneer_whitelabel',
                actionStatus = ABACUS_ACTION_STATUSES.COMPLETE,
            }: {
                entityName: string;
                countryCode: string;
                paymentType?: string;
                paymentService?: string;
                actionStatus?: string;
            }) =>
                ({
                    ...defaultData,
                    abacusAccount: {
                        ...defaultData.abacusAccount,
                        accountPaymentTerm: {
                            paymentType,
                            paymentEntity: {
                                paymentEntityName: entityName,
                                countryOfTaxReporting: countryCode,
                            },
                        },
                        accountPayee: {
                            ...defaultData.abacusAccount.accountPayee,
                            referencePaymentType: {
                                paymentService,
                                referencePaymentTypeId: '8',
                            },
                            actionStates: [
                                {
                                    actionName: ABACUS_ACTIONS.TAX_ELIGIBILITY,
                                    actionStatus,
                                },
                            ],
                        },
                    },
                }) as unknown as GetAccountFullDetailQuery;

            it('shows Whitelabel flow for USA country code when FF is ENABLED (even if entity is NO)', () => {
                const accountFullDetail = getAccountData({
                    entityName: PAYMENT_ENTITY_NAMES.ORCHARD_NO,
                    countryCode: USA_ISO_COUNTRY_CODE,
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.getByTestId('TaxFormInfoTopSection')
                ).toBeInTheDocument();
                expect(
                    screen.queryByTestId('accountTaxInfo')
                ).not.toBeInTheDocument();
            });

            it('shows Default flow for USA country code when FF is DISABLED and entity is NO', () => {
                const accountFullDetail = getAccountData({
                    entityName: PAYMENT_ENTITY_NAMES.ORCHARD_NO,
                    countryCode: USA_ISO_COUNTRY_CODE,
                });

                render({ accountFullDetail });

                expect(
                    screen.getByTestId('accountTaxInfo')
                ).toBeInTheDocument();
            });

            it('shows Whitelabel flow for ORCHARD-US even if FF is DISABLED', () => {
                const accountFullDetail = getAccountData({
                    entityName: PAYMENT_ENTITY_NAMES.ORCHARD_US,
                    countryCode: 'ANY',
                });

                render({ accountFullDetail });

                expect(
                    screen.getByTestId('TaxFormInfoTopSection')
                ).toBeInTheDocument();
                expect(
                    screen.queryByTestId('accountTaxInfo')
                ).not.toBeInTheDocument();
            });

            it('shows Whitelabel flow for AWAL-US when FF is ENABLED (paymentService=payoneer_whitelabel drives the gate, even if legacy paymentType says PAYONEER)', () => {
                const accountFullDetail = getAccountData({
                    entityName: 'AWAL-US',
                    countryCode: USA_ISO_COUNTRY_CODE,
                    paymentType: PaymentType.PAYONEER, // legacy field mis-classifies AWAL
                    paymentService: 'payoneer_whitelabel', // canonical truth
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.getByTestId('TaxFormInfoTopSection')
                ).toBeInTheDocument();
                expect(
                    screen.queryByTestId('accountTaxInfo')
                ).not.toBeInTheDocument();
            });

            it('shows Default flow for AWAL-US when FF is DISABLED (legacy resolver path preserved)', () => {
                const accountFullDetail = getAccountData({
                    entityName: 'AWAL-US',
                    countryCode: USA_ISO_COUNTRY_CODE,
                    paymentType: PaymentType.PAYONEER, // legacy: AWAL → PAYONEER
                    paymentService: 'payoneer_whitelabel', // ignored when FF off
                });

                render({ accountFullDetail });

                expect(
                    screen.getByTestId('accountTaxInfo')
                ).toBeInTheDocument();
            });

            it('shows Default flow for ORCHARD-US when FF is ENABLED and paymentService is non-whitelabel (e.g. paychex), even if legacy paymentType says PAYONEER_WHITELABEL', () => {
                const accountFullDetail = getAccountData({
                    entityName: PAYMENT_ENTITY_NAMES.ORCHARD_US,
                    countryCode: USA_ISO_COUNTRY_CODE,
                    paymentType: PaymentType.PAYONEER_WHITELABEL, // legacy over-claims
                    paymentService: 'paychex', // canonical truth
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.getByTestId('accountTaxInfo')
                ).toBeInTheDocument();
            });

            it('grants canEditUSTaxInfo for AWAL-US when FF is ENABLED (Add Tax Form details button visible in PENDING state)', () => {
                const accountFullDetail = getAccountData({
                    entityName: 'AWAL-US',
                    countryCode: USA_ISO_COUNTRY_CODE,
                    paymentType: PaymentType.PAYONEER,
                    paymentService: 'payoneer_whitelabel',
                    actionStatus: ABACUS_ACTION_STATUSES.INIT,
                });

                (useCanPerformAction as jest.Mock).mockReturnValue({
                    data: true,
                    loading: false,
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.getByText('Add Tax Form details')
                ).toBeInTheDocument();
            });

            it('shows Whitelabel flow for ORCHARD-US when FF is ENABLED (high-volume case, paymentService=payoneer_whitelabel)', () => {
                const accountFullDetail = getAccountData({
                    entityName: PAYMENT_ENTITY_NAMES.ORCHARD_US,
                    countryCode: USA_ISO_COUNTRY_CODE,
                    paymentType: PaymentType.PAYONEER_WHITELABEL,
                    paymentService: 'payoneer_whitelabel',
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.getByTestId('TaxFormInfoTopSection')
                ).toBeInTheDocument();
                expect(
                    screen.queryByTestId('accountTaxInfo')
                ).not.toBeInTheDocument();
            });

            it('shows Default flow for AWAL-UK whitelabel when FF is ENABLED (UK has no whitelabel tax-form UI yet)', () => {
                const accountFullDetail = getAccountData({
                    entityName: PAYMENT_ENTITY_NAMES.AWAL_UK,
                    countryCode: 'GBR',
                    paymentType: PaymentType.PAYONEER,
                    paymentService: 'payoneer_whitelabel',
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.getByTestId('accountTaxInfo')
                ).toBeInTheDocument();
            });

            it('blocks canEditUSTaxInfo for AWAL-UK whitelabel when FF is ENABLED (country !== USA)', () => {
                const accountFullDetail = getAccountData({
                    entityName: PAYMENT_ENTITY_NAMES.AWAL_UK,
                    countryCode: 'GBR',
                    paymentType: PaymentType.PAYONEER,
                    paymentService: 'payoneer_whitelabel',
                    actionStatus: ABACUS_ACTION_STATUSES.INIT,
                });

                (useCanPerformAction as jest.Mock).mockReturnValue({
                    data: true,
                    loading: false,
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.queryByText('Add Tax Form details')
                ).not.toBeInTheDocument();
            });

            it('falls back to Default flow when paymentService is null under FF ENABLED (defensive: federation gap)', () => {
                const accountFullDetail = getAccountData({
                    entityName: 'AWAL-US',
                    countryCode: USA_ISO_COUNTRY_CODE,
                    paymentType: PaymentType.PAYONEER,
                    paymentService: null as unknown as string,
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.getByTestId('accountTaxInfo')
                ).toBeInTheDocument();
            });

            it('blocks canEditUSTaxInfo for AWAL-US when FF is DISABLED (Add Tax Form details button hidden)', () => {
                const accountFullDetail = getAccountData({
                    entityName: 'AWAL-US',
                    countryCode: USA_ISO_COUNTRY_CODE,
                    paymentType: PaymentType.PAYONEER_WHITELABEL,
                    paymentService: 'payoneer_whitelabel',
                    actionStatus: ABACUS_ACTION_STATUSES.INIT,
                });

                (useCanPerformAction as jest.Mock).mockReturnValue({
                    data: true,
                    loading: false,
                });

                render({ accountFullDetail });

                expect(
                    screen.queryByText('Add Tax Form details')
                ).not.toBeInTheDocument();
            });

            it('always shows Default flow for UK, regardless of country code', () => {
                const accountFullDetail = getAccountData({
                    entityName: PAYMENT_ENTITY_NAMES.ORCHARD_UK,
                    countryCode: 'GBR',
                });

                render(
                    { accountFullDetail },
                    createIdentity({
                        features: {
                            [USER_FEATURES.TAP_AWAL_PAYONEER_MIGRATION]: true,
                        },
                    })
                );

                expect(
                    screen.getByTestId('accountTaxInfo')
                ).toBeInTheDocument();
            });
        });
    });

    describe('ORCHARD-ES tax form logic', () => {
        const baseESData = {
            ...accountDetail,
            abacusAccount: {
                ...accountDetail.abacusAccount,
                accountPaymentTerm: {
                    ...accountDetail.abacusAccount.accountPaymentTerm,
                    paymentType: PaymentType.PAYONEER_WHITELABEL, // Component logic uses this and paymentEntityName
                    paymentEntity: {
                        paymentEntityName: 'ORCHARD-ES',
                        referencePaymentEntityId: '321',
                    },
                },
                accountTaxInfo: {
                    ...accountDetail.abacusAccount.accountTaxInfo,
                    countryOfTaxResidence: COUNTRY_NAMES.ESP.ISO3,
                },
                // Ensure actionStates is present, but TAX_ELIGIBILITY status doesn't force COMPLETED view
                accountPayee: {
                    ...accountDetail.abacusAccount.accountPayee,
                    referencePaymentType: {
                        paymentService: 'payoneer_whitelabel',
                        referencePaymentTypeId: '8',
                    },
                    actionStates: [
                        {
                            abacusStateId: '12345',
                            actionName: ABACUS_ACTIONS.TAX_ELIGIBILITY,
                            actionStatus: ABACUS_ACTION_STATUSES.COMPLETE,
                            createdAt: '',
                            lastModified: '',
                            message: '',
                        },
                    ],
                },
            },
        };

        it('shows COMPLETED view when ES tax info is complete', () => {
            // Mock permission check
            (useCanPerformAction as jest.Mock).mockReturnValue({
                data: true,
                loading: false,
            });
            // Mock ES query to return complete data
            (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
                data: {
                    abacusAccount: {
                        accountPayee: {
                            taxDetails: {
                                localTaxId: '123456789',
                            },
                        },
                        accountTaxInfo: {
                            countryOfTaxResidence: COUNTRY_NAMES.ESP.ISO3,
                        },
                    },
                },
                loading: false,
            });

            renderInAppContext(
                <TaxFormInfo
                    accountFullDetailQueryLoading={false}
                    accountFullDetail={baseESData}
                />
            );

            expect(
                screen.getByTestId('TaxFormInfoCompleted')
            ).toBeInTheDocument();
            expect(
                screen.queryByTestId('accountTaxInfo')
            ).not.toBeInTheDocument();
        });

        it('shows EXPIRED view when ES tax info is rejected due to certificate expiration', () => {
            // Mock permission check
            (useCanPerformAction as jest.Mock).mockReturnValue({
                data: true,
                loading: false,
            });
            // Mock ES query to return complete data
            (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
                data: {
                    abacusAccount: {
                        accountPayee: {
                            taxDetails: {
                                localTaxId: '123456789',
                            },
                            actionStates: [
                                {
                                    abacusStateId: '12345',
                                    actionName: ABACUS_ACTIONS.TAX_ELIGIBILITY,
                                    actionStatus:
                                        ABACUS_ACTION_STATUSES.REJECTED,
                                    createdAt: '',
                                    lastModified: '',
                                    message: '',
                                },
                            ],
                        },
                        accountTaxInfo: {
                            countryOfTaxResidence: COUNTRY_NAMES.ALB.ISO3,
                            certificateOfResidenceExpirationDate: '2025-01-01',
                        },
                    },
                },
                loading: false,
            });
            const rejectedESData = {
                ...accountDetail,
                abacusAccount: {
                    ...accountDetail.abacusAccount,
                    accountPaymentTerm: {
                        ...accountDetail.abacusAccount.accountPaymentTerm,
                        paymentType: PaymentType.PAYONEER_WHITELABEL,
                        paymentEntity: {
                            paymentEntityName: 'ORCHARD-ES',
                            referencePaymentEntityId: '321',
                        },
                    },
                    accountTaxInfo: {
                        ...accountDetail.abacusAccount.accountTaxInfo,
                        countryOfTaxResidence: COUNTRY_NAMES.ALB.ISO3,
                    },
                    accountPayee: {
                        ...accountDetail.abacusAccount.accountPayee,
                        referencePaymentType: {
                            paymentService: 'payoneer_whitelabel',
                            referencePaymentTypeId: '8',
                        },
                        actionStates: [
                            {
                                abacusStateId: '12345',
                                actionName: ABACUS_ACTIONS.TAX_ELIGIBILITY,
                                actionStatus: ABACUS_ACTION_STATUSES.REJECTED,
                                createdAt: '',
                                lastModified: '',
                                message: '',
                            },
                        ],
                    },
                },
            };

            renderInAppContext(
                <TaxFormInfo
                    accountFullDetailQueryLoading={false}
                    accountFullDetail={rejectedESData}
                />
            );
            expect(
                screen.getByText(/Account is not eligible for payment as the/i)
            ).toBeInTheDocument();
            expect(
                screen.getByText(/expired on 2025-01-01/i)
            ).toBeInTheDocument();
            expect(
                screen.getByTestId('TaxFormInfoCompleted')
            ).toBeInTheDocument();
            expect(
                screen.queryByTestId('accountTaxInfo')
            ).not.toBeInTheDocument();
            expect(screen.getByTestId('openModalBtn')).toBeInTheDocument();
        });

        it('renders SkeletonLoader while taxInfoES query is loading', () => {
            // Mock ES query as loading
            (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
                data: undefined,
                loading: true,
            });

            renderInAppContext(
                <TaxFormInfo
                    accountFullDetailQueryLoading={false}
                    accountFullDetail={baseESData}
                />
            );

            expect(screen.getByTestId('SkeletonLoader')).toBeInTheDocument();
            expect(
                screen.queryByTestId('TaxFormInfoCompleted')
            ).not.toBeInTheDocument();
            expect(
                screen.queryByTestId('accountTaxInfo')
            ).not.toBeInTheDocument();
        });
    });

    describe('ORCHARD-UK and ORCHARD-NO tax details', () => {
        const renderByPaymentEntityName = (
            paymentEntityName = PAYMENT_ENTITY_NAMES.ORCHARD_UK
        ) => {
            (useCanPerformAction as jest.Mock).mockReturnValue({
                data: false,
                loading: false,
            });
            const modifiedData = {
                ...accountDetail,
                abacusAccount: {
                    ...accountDetail.abacusAccount,
                    accountPaymentTerm: {
                        ...accountDetail.abacusAccount.accountPaymentTerm,
                        paymentType: PaymentType.PAYONEER_WHITELABEL,
                        paymentEntity: {
                            ...accountDetail.abacusAccount.accountPaymentTerm
                                .paymentEntity,
                            paymentEntityName: paymentEntityName,
                        },
                    },
                    accountPayee: {
                        ...accountDetail.abacusAccount.accountPayee,
                        referencePaymentType: {
                            paymentService: 'payoneer_whitelabel',
                            referencePaymentTypeId: '8',
                        },
                        actionStates: [
                            {
                                abacusStateId: '36546',
                                actionName: ABACUS_ACTIONS.TAX_ELIGIBILITY,
                                actionStatus: 'COMPLETE',
                                createdAt: '',
                                lastModified: '',
                                message: '',
                            },
                        ],
                    },
                },
            };
            (useAccountPayeeTaxFormInfoESQuery as jest.Mock).mockReturnValue({
                data: undefined,
                loading: false,
            });

            renderInAppContext(
                <TaxFormInfo
                    accountFullDetailQueryLoading={false}
                    accountFullDetail={modifiedData}
                />
            );
        };

        test('render AccountTaxInfoDetail component for UK tax experience', () => {
            renderByPaymentEntityName();
            expect(screen.getByTestId('accountTaxInfo')).toBeInTheDocument();
        });

        test('render AccountTaxInfoDetail component for NO tax experience', () => {
            renderByPaymentEntityName(PAYMENT_ENTITY_NAMES.ORCHARD_NO);
            expect(screen.getByTestId('accountTaxInfo')).toBeInTheDocument();
        });
    });
});
