import React from 'react';
import { screen } from '@testing-library/react';
import {
    ABACUS_ACTION_STATUSES,
    ABACUS_ACTIONS,
} from '@theorchard/accounting-apps-shared';
import { type Identity } from '@theorchard/suite-frontend';
import { renderInAppContext } from '@theorchard/suite-testing';
import PaymentEligibilityStatus, {
    type EligibilityStatuses,
    type PaymentEligibilityStatusProps,
} from 'src/components/account-detail/payment-eligibility-status';
import { ELIGIBILITY_STATUSES } from 'src/constants';

describe('<PaymentEligibilityStatus>', () => {
    const renderComponent = (
        props: PaymentEligibilityStatusProps['accountDetails'],
        identity?: Identity
    ) =>
        renderInAppContext(
            <PaymentEligibilityStatus accountDetails={props} />,
            { identity }
        );

    describe('Regular account', () => {
        const defaultProps: PaymentEligibilityStatusProps['accountDetails'] = {
            accountPayee: {
                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: '',
                    },
                ],
                referencePaymentType: {
                    paymentService: '',
                },
            },
            accountPaymentTerm: {
                paymentType: 'PAYONEER',
            },
            eligibilityStatus:
                ELIGIBILITY_STATUSES.ACTIVE as EligibilityStatuses,
        };

        it('renders Active status', () => {
            renderComponent(defaultProps);
            expect(screen.getByTestId('eligibilityStatusActive')).toBeDefined();
        });

        it('renders On Hold status', () => {
            const onHoldProps: PaymentEligibilityStatusProps['accountDetails'] =
                {
                    ...defaultProps,
                    eligibilityStatus:
                        ELIGIBILITY_STATUSES.ON_HOLD as EligibilityStatuses,
                };
            renderComponent(onHoldProps);
            expect(screen.getByTestId('eligibilityStatusOnHold')).toBeDefined();
        });

        it('renders Ineligible status', () => {
            const ineligibleProps = {
                ...defaultProps,
            };
            ineligibleProps.accountPayee.actionStates[0].actionStatus =
                ABACUS_ACTION_STATUSES.INIT;
            renderComponent(ineligibleProps);
            expect(
                screen.getByTestId('eligibilityStatusIneligible')
            ).toBeDefined();
        });
    });

    describe('Whitelabel account', () => {
        const mockIdentity = {
            features: {},
            id: '',
        };
        const defaultProps: PaymentEligibilityStatusProps['accountDetails'] = {
            accountPayee: {
                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.BANKING_DETAILS_REVIEW,
                        actionStatus: ABACUS_ACTION_STATUSES.APPROVED,
                        abacusStateId: '',
                        message: '',
                    },
                ],
                referencePaymentType: {
                    paymentService: 'payoneer_whitelabel',
                },
            },
            accountPaymentTerm: {
                paymentType: 'PAYONEER',
            },
            eligibilityStatus:
                ELIGIBILITY_STATUSES.ACTIVE as EligibilityStatuses,
        };

        it('renders Active status', () => {
            renderComponent(defaultProps, mockIdentity);
            expect(screen.getByTestId('eligibilityStatusActive')).toBeDefined();
        });

        it('renders On Hold status', () => {
            const onHoldProps = {
                ...defaultProps,
                eligibilityStatus:
                    ELIGIBILITY_STATUSES.ON_HOLD as EligibilityStatuses,
            };
            renderComponent(onHoldProps, mockIdentity);
            expect(screen.getByTestId('eligibilityStatusOnHold')).toBeDefined();
        });

        it('renders Ineligible status', () => {
            const ineligibleProps = {
                ...defaultProps,
            };
            ineligibleProps.accountPayee.actionStates[2].actionStatus =
                ABACUS_ACTION_STATUSES.INIT;
            renderComponent(ineligibleProps, mockIdentity);
            expect(
                screen.getByTestId('eligibilityStatusIneligible')
            ).toBeDefined();
        });

        it('renders Ineligible no action', () => {
            const ineligibleProps = {
                ...defaultProps,
            };
            ineligibleProps.accountPayee.actionStates.splice(2, 1);
            renderComponent(ineligibleProps, mockIdentity);
            expect(
                screen.getByTestId('eligibilityStatusIneligible')
            ).toBeDefined();
        });
    });
});
