import React from 'react';
import { fireEvent, screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import { contractFlowthroughDetail } from 'src/__fixtures__/graphql/contract-flowthrough';
import {
    CONTRACT_FLOWTHROUGH_ALERT_INFO,
    NO_CONTRACT_FLOWTHROUGH_MSG,
} from 'src/apollo/type-constants/contract-flowthrough';
import {
    ContractFlowthrough,
    ContractFlowthroughProps,
} from 'src/components/contract-detail/contract-flowthrough';

describe('<ContractFlowthrough />', () => {
    const render = (props: ContractFlowthroughProps) =>
        renderInAppContext(<ContractFlowthrough {...props} />);
    const abacusContractFlowthrough =
        contractFlowthroughDetail.abacusContractFlowthrough;

    it('renders Message if flowthrough is not added to the contract', () => {
        render({ contractFlowthrough: null });

        expect(screen.getByText(NO_CONTRACT_FLOWTHROUGH_MSG)).toBeDefined();
    });

    it('renders flowthrough details when using rate', () => {
        render({
            contractFlowthrough: abacusContractFlowthrough,
        });

        expect(screen.getByText('Rate')).toBeDefined();
        expect(screen.getByText('Calculation')).toBeDefined();
        expect(screen.getByText('Recoupment Cap')).toBeDefined();
        expect(screen.getByText('Status')).toBeDefined();
        expect(screen.queryByText('Calculation Comment')).toBeNull();

        expect(screen.getByText('12.1%')).toBeDefined();
        expect(
            screen.getByText('(Net Revenue - Expenses) * FT%')
        ).toBeDefined();
        expect(screen.getByText('-')).toBeDefined();
        expect(screen.getByText('Active')).toBeDefined();
    });

    it('renders flowthrough details when using recoupment cap', () => {
        const contractFlowthrough = {
            ...abacusContractFlowthrough,
            referenceFlowthroughCalculation: {
                flowthroughCalculation:
                    'Net Revenue + Adjustments - Recoupment Cap',
                referenceFlowthroughCalculationId: '5',
            },
        };

        render({
            contractFlowthrough,
        });

        expect(screen.getByText('Rate')).toBeDefined();
        expect(screen.getByText('Calculation')).toBeDefined();
        expect(screen.getByText('Recoupment Cap')).toBeDefined();
        expect(screen.getByText('Status')).toBeDefined();
        expect(screen.queryByText('Calculation Comment')).toBeNull();

        expect(screen.getByText('-')).toBeDefined();
        expect(
            screen.getByText('Net Revenue + Adjustments - Recoupment Cap')
        ).toBeDefined();
        expect(screen.getByText('12901')).toBeDefined();
        expect(screen.getByText('Active')).toBeDefined();
    });

    it('shows tooltip on hover for the rate', async () => {
        render({
            contractFlowthrough: abacusContractFlowthrough,
        });

        const rateTooltip = screen.getByTestId('rateTooltip');
        fireEvent.mouseEnter(rateTooltip);
        const tooltipText = await screen.findByTestId('rateTooltipOverlay');
        expect(tooltipText).toBeInTheDocument();
        expect(tooltipText).toHaveTextContent("Client's Share");
    });

    it('shows tooltip on hover for the status', async () => {
        render({
            contractFlowthrough: abacusContractFlowthrough,
        });

        const statusTooltip = screen.getByTestId('statusTooltip');
        fireEvent.mouseEnter(statusTooltip);
        const tooltipText = await screen.findByTestId('statusTooltipOverlay');
        expect(tooltipText).toBeInTheDocument();
        expect(tooltipText).toHaveTextContent('2025-03-25 by Test User');
    });

    it('hides tooltip for status if there is no modified status', () => {
        render({
            contractFlowthrough: {
                ...abacusContractFlowthrough,
                statusLastModified: null,
                statusLastModifiedBy: null,
                statusLastModifiedByIdentity: null,
            },
        });

        expect(screen.queryByTestId('statusTooltip')).toBeNull();
    });

    it('renders read only alert message', async () => {
        render({
            contractFlowthrough: abacusContractFlowthrough,
        });

        expect(screen.getByText(CONTRACT_FLOWTHROUGH_ALERT_INFO)).toBeDefined();
    });

    it('renders flowthrough details when using manual', () => {
        const contractFlowthrough = {
            ...abacusContractFlowthrough,
            calculationComment: 'Using different calculation method',
            referenceFlowthroughCalculation: {
                flowthroughCalculation: 'Manual',
                flowthroughCalculationName: 'manual',
                referenceFlowthroughCalculationId: '6',
            },
        };

        render({
            contractFlowthrough,
        });

        expect(screen.getByText('Rate')).toBeDefined();
        expect(screen.getByText('Calculation')).toBeDefined();
        expect(screen.getByText('Recoupment Cap')).toBeDefined();
        expect(screen.getByText('Status')).toBeDefined();
        expect(screen.getByText('Calculation Comment')).toBeDefined();

        expect(screen.getAllByText('-')).toBeDefined();
        expect(screen.getByText('Manual')).toBeDefined();
        expect(screen.getByText('Active')).toBeDefined();

        expect(
            screen.getByText('Using different calculation method')
        ).toBeDefined();
    });
});
