import React from 'react';
import { fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderInAppContext } from '@theorchard/suite-testing';
import * as CurrentValidationProvider from 'src/components/currentValidationProvider/currentValidationProvider';
import { useTracksQuery } from 'src/data/queries/tracks';
import PotentialAudioInfringementModal from '../potentialAudioInfringementModal';

jest.mock('src/data/queries/tracks', () => ({
    useTracksQuery: jest.fn(),
}));

describe('<PotentialAudioInfringementModal>', () => {
    const potentialAudioInfringementMatches = [
        {
            artists: ['Test artist 1'],
            label: 'Chill label',
            title: 'Some Song',
            songwhip_external_links: {
                spotify: [{ link: 'https://spotify.com' }],
            },
            rights_claim: [
                {
                    distributor: {
                        id: 'A123',
                        name: 'Test Distributor 1',
                    },
                },
            ],
        },
    ];

    const crossAccountOSRConflictMatches = [
        {
            tuid: 1,
            product_id: 2,
            upc: '321',
            vendor_id: 10,
            isrc: 'abc',
            company_brand: 'test_brand_orchard',
        },
    ];

    const trackMetadata = {
        trackName: 'Other song',
        label: 'Cool label',
        artists: ['Test artist 2'],
        imprint: 'Cool imprint',
    };

    // eslint-disable-next-line @typescript-eslint/no-empty-function
    const mockOnRequestClose = () => {};

    beforeEach(() => {
        // Mock implementation of useTracksQuery
        (useTracksQuery as jest.Mock).mockReturnValue({
            data: [
                {
                    tuid: '1',
                    trackName: 'Mock Track Name',
                    participations: [
                        {
                            participant: { name: 'Mock Artist' },
                        },
                    ],
                    product: { imprint: 'Mock Imprint' },
                },
            ],
            loading: false,
        });
    });

    afterEach(() => jest.resetAllMocks());

    test('matches snapshot', () => {
        const component = renderInAppContext(
            <PotentialAudioInfringementModal
                isOpen={true}
                onRequestClose={mockOnRequestClose}
                potentialAudioInfringementMatches={
                    potentialAudioInfringementMatches
                }
                crossAccountOSRConflictMatches={crossAccountOSRConflictMatches}
                trackMetadata={trackMetadata}
            />
        );
        expect(component).toMatchSnapshot();
    });

    test('displays CrossAccountOSRConflictMatches correctly', () => {
        const { getByText, getByTestId } = renderInAppContext(
            <PotentialAudioInfringementModal
                isOpen={true}
                onRequestClose={mockOnRequestClose}
                potentialAudioInfringementMatches={[]}
                crossAccountOSRConflictMatches={crossAccountOSRConflictMatches}
                trackMetadata={trackMetadata}
            />
        );

        const expandableRow = getByText('Mock Track Name');

        userEvent.click(expandableRow);

        expect(getByText('Mock Track Name')).toBeVisible();
        expect(getByText('Mock Artist')).toBeVisible();
        expect(getByText('Mock Imprint')).toBeVisible();
        expect(getByText('Account ID: 10')).toBeVisible();
        expect(getByText('test_brand_orchard')).toBeVisible();
        expect(
            getByTestId(
                'PotentialAudioInfringementModal-MatchesTable-dspRow-321'
            ).querySelector('a')
        ).toHaveAttribute(
            'href',
            'https://oa.qaorch.com/cont_mgmt/edit_track.php?upc=321&tuid=1'
        );
    });

    test('displays PotentialAudioInfringementMatches correctly', () => {
        const { getByText } = renderInAppContext(
            <PotentialAudioInfringementModal
                isOpen={true}
                onRequestClose={mockOnRequestClose}
                potentialAudioInfringementMatches={
                    potentialAudioInfringementMatches
                }
                crossAccountOSRConflictMatches={[]}
                trackMetadata={trackMetadata}
            />
        );

        expect(getByText('Some Song')).toBeVisible();
        expect(getByText('Test artist 1')).toBeVisible();
        expect(getByText('Chill label')).toBeVisible();
        expect(getByText('Test Distributor 1')).toBeVisible();
        expect(getByText('Links')).toBeVisible();
    });

    test('calls dispatchValidation with expected params when modal is closed', () => {
        const mockDispatch = jest.fn();
        const currentValidationTestValue = 'track-123';

        jest.spyOn(
            CurrentValidationProvider,
            'useCurrentValidationContext'
        ).mockReturnValue({
            state: { currentValidation: currentValidationTestValue },
            dispatch: mockDispatch,
        });

        const { getByTestId } = renderInAppContext(
            <PotentialAudioInfringementModal
                isOpen={true}
                onRequestClose={mockOnRequestClose}
                potentialAudioInfringementMatches={
                    potentialAudioInfringementMatches
                }
                crossAccountOSRConflictMatches={[]}
                trackMetadata={trackMetadata}
            />
        );

        const closeButton = getByTestId('CloseGlyphIcon');

        fireEvent.click(closeButton);

        expect(mockDispatch).toHaveBeenCalledWith({
            currentValidation: currentValidationTestValue,
            triggerScroll: true,
            showPopover: true,
        });
    });
});
