import React from 'react';
import { screen } from '@testing-library/react';
import {
    ABACUS_ACTION_STATUSES,
    ABACUS_ACTIONS,
} from '@theorchard/accounting-apps-shared';
import { renderInAppContext } from '@theorchard/suite-testing';
import EligibilityAlert, {
    type EligibilityAlertProps,
} from 'src/components/account-detail/eligibility-alert';

describe('<EligibilityAlert>', () => {
    const defaultProps: EligibilityAlertProps = {
        actionStates: [
            {
                actionName: ABACUS_ACTIONS.TAX_ELIGIBILITY,
                actionStatus: ABACUS_ACTION_STATUSES.COMPLETE,
                abacusStateId: '',
                message: '',
            },
            {
                actionName: ABACUS_ACTIONS.PAYMENT_ELIGIBILITY,
                actionStatus: ABACUS_ACTION_STATUSES.APPROVED,
                abacusStateId: '',
                message: '',
            },
        ],
        actionName: ABACUS_ACTIONS.PAYMENT_ELIGIBILITY,
    };

    const renderComponent = (props: EligibilityAlertProps) =>
        renderInAppContext(<EligibilityAlert {...props} />);

    it('renders Alert if payment state is not approved', () => {
        const paymentIneligibleProps = JSON.parse(JSON.stringify(defaultProps));
        paymentIneligibleProps.actionStates[1].actionStatus =
            ABACUS_ACTION_STATUSES.INIT;
        renderComponent(paymentIneligibleProps);
        expect(screen.getByTestId('eligibilityAlertTestid')).toBeDefined();
        expect(screen.getByText('Missing payment info')).toBeDefined();
    });

    it('displays Detail message for Declined status', () => {
        const taxIneligibleProps = JSON.parse(JSON.stringify(defaultProps));
        taxIneligibleProps.actionStates[0].actionStatus =
            ABACUS_ACTION_STATUSES.REJECTED;
        taxIneligibleProps.actionStates[0].message = 'Test Description';
        taxIneligibleProps.actionName = ABACUS_ACTIONS.TAX_ELIGIBILITY;
        renderComponent(taxIneligibleProps);
        expect(
            screen.getByTestId('eligibilityAlertDetailTestid')
        ).toBeDefined();
        expect(screen.getByText('Test Description')).toBeDefined();
    });

    it('renders Alert if tax state is not completed', () => {
        const taxIneligibleProps = JSON.parse(JSON.stringify(defaultProps));
        taxIneligibleProps.actionStates[0].actionStatus =
            ABACUS_ACTION_STATUSES.INIT;
        taxIneligibleProps.actionName = ABACUS_ACTIONS.TAX_ELIGIBILITY;
        renderComponent(taxIneligibleProps);
        expect(screen.getByTestId('eligibilityAlertTestid')).toBeDefined();
        expect(screen.getByText('Missing tax info')).toBeDefined();
    });

    it('does not render anything for eligible state', () => {
        renderComponent(defaultProps);
        expect(
            screen.queryAllByTestId('eligibilityAlertTestid').length
        ).toEqual(0);
    });
});
