import React from 'react';
import { fireEvent, screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import { CONTRACT_TYPES } from 'src/constants';
import {
    ContractHistoryReportsLink,
    ContractHistoryReportsLinkProps,
} from 'src/components/contract-detail/contract-history-reports-link';

describe('<ContractHistoryReportsLink />', () => {
    const render = (props: ContractHistoryReportsLinkProps) =>
        renderInAppContext(<ContractHistoryReportsLink {...props} />);

    const props = {
        className: 'HistoryReportsLink',
        contractId: 30,
        contractType: CONTRACT_TYPES.DISTRIBUTION,
    };
    it('opens modal upon click', () => {
        render(props);

        const historyReportLink = screen.getByTestId(
            'contractHistoryReportsLink'
        );
        fireEvent.click(historyReportLink);

        expect(
            screen.getByText(
                'History reports will be generated outside of Abacus in Looker.'
            )
        ).toBeDefined();
    });

    it('renders links to Looker reports for Distribution type contracts', () => {
        render(props);

        const historyReportLink = screen.getByTestId(
            'contractHistoryReportsLink'
        );
        fireEvent.click(historyReportLink);

        const generalReportLink = screen.getByRole('link', {
            name: 'General Report',
        });
        expect(generalReportLink).toHaveAttribute(
            'href',
            'https://theorchard.looker.com/looks/21866?f[royalty_accounting_prod_vw_fact_contract_history.contract_id]=30'
        );

        const termsAndConditionsReportLink = screen.getByRole('link', {
            name: 'Terms & Conditions Report',
        });
        expect(termsAndConditionsReportLink).toHaveAttribute(
            'href',
            'https://theorchard.looker.com/looks/21868?f[royalty_accounting_prod_vw_fact_contract_history.contract_id]=30'
        );
    });

    it('renders links to Looker reports for KNR/neighbouring rights contracts', () => {
        render({
            ...props,
            contractId: 35,
            contractType: CONTRACT_TYPES.NEIGHBOURING_RIGHTS,
        });

        const historyReportLink = screen.getByTestId(
            'contractHistoryReportsLink'
        );
        fireEvent.click(historyReportLink);

        const generalReportLink = screen.getByRole('link', {
            name: 'General Report',
        });
        expect(generalReportLink).toHaveAttribute(
            'href',
            'https://theorchard.looker.com/looks/21866?f[royalty_accounting_prod_vw_fact_contract_history.contract_id]=35'
        );

        const termsAndConditionsReportLink = screen.getByRole('link', {
            name: 'Terms & Conditions Report',
        });
        expect(termsAndConditionsReportLink).toHaveAttribute(
            'href',
            'https://theorchard.looker.com/looks/21867?f[royalty_accounting_prod_vw_fact_contract_history.contract_id]=35'
        );
    });
});
