import React from 'react';
import { screen } from '@testing-library/react';
import { type Identity } from '@theorchard/suite-frontend';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import AccountTaxInfoDetail, {
    type AccountTaxInfoDetailProps,
} from 'src/components/account-detail/account-tax-info-detail';
import { USER_FEATURES } from 'src/constants';

describe('<AccountTaxInfoDetail>', () => {
    const accountTaxInfoId = '1';
    const defaultProps: AccountTaxInfoDetailProps = {
        accountTaxInfo: {
            accountTaxInfoId,
            countryOfTaxResidence: 'GBR',
            isSbaSigned: true,
            isVatExempt: true,
            isTaxTreatyClaimed: true,
        },
        accountTaxInfoHistory: [
            {
                accountTaxInfoHistoryId: 1,
                countryOfTaxResidence: 'DEU',
                createdAt: '2020-10-10',
                lastModified: '2020-10-11',
            },
            {
                accountTaxInfoHistoryId: 2,
                countryOfTaxResidence: 'UKR',
                createdAt: '2020-10-10',
                lastModified: '2020-10-12',
            },
        ],
        actionStates: [],
    };

    const renderComponent = (
        props: AccountTaxInfoDetailProps,
        identity?: Identity
    ) => renderInAppContext(<AccountTaxInfoDetail {...props} />, { identity });

    it('renders country name', () => {
        renderComponent(defaultProps);
        expect(screen.getByText('United Kingdom')).toBeDefined();
    });

    it('renders isSbaSigned if country is GBR', () => {
        renderComponent(defaultProps);
        expect(screen.getByText('Self Billing Agreement VAT')).toBeDefined();
    });

    it('does not render isSbaSigned if country is not GBR', () => {
        const newProps: AccountTaxInfoDetailProps = {
            ...defaultProps,
            accountTaxInfo: {
                accountTaxInfoId,
                countryOfTaxResidence: 'USA',
                isSbaSigned: false,
            },
        };
        renderComponent(newProps);
        expect(screen.getByText('United States of America')).toBeDefined();
        expect(screen.queryByText('Self Billing Agreement VAT')).toBeFalsy();
    });

    it('renders isTaxTreatyClaimed feature enabled', () => {
        const mockIdentity = createIdentity({
            features: {
                [USER_FEATURES.ABACUS_TAP_SHOW_TAX_TREATY_CLAIMED]: true,
            },
        });
        renderComponent(defaultProps, mockIdentity);
        expect(screen.getByText('Tax Treaty Claimed')).toBeDefined();
        expect(screen.getAllByText('Yes')).toBeDefined();
    });

    it('renders isTaxTreatyClaimed feature disabled', () => {
        const mockIdentity = createIdentity({
            features: {
                [USER_FEATURES.ABACUS_TAP_SHOW_TAX_TREATY_CLAIMED]: false,
            },
        });
        renderComponent(defaultProps, mockIdentity);
        expect(screen.queryByText('Tax Treaty Claimed')).toBeNull();
    });

    describe('Account tax info history table', () => {
        it('renders tax info history table', () => {
            renderComponent(defaultProps);
            expect(
                screen.getByText('Country of tax residence history')
            ).toBeVisible();
            expect(screen.getByText('Start Date')).toBeVisible();
            expect(screen.getByText('End Date')).toBeVisible();
        });

        it('renders tax info history table with empty state', () => {
            const emptyHistoryProps = {
                ...defaultProps,
                accountTaxInfoHistory: [],
            };
            renderComponent(emptyHistoryProps);
            expect(
                screen.getByText('There are no prior entries')
            ).toBeVisible();
        });
    });
});
