import React from 'react';
import { fireEvent } from '@testing-library/react';
import { Segment } from '@theorchard/suite-frontend';
import { renderInAppContext } from '@theorchard/suite-testing';
import * as queries from 'src/data/queries/getRecentOwnershipNrIngestions/getRecentOwnershipNrIngestions';
import { type NrOwnershipIngestionFile } from 'src/data/queries/getRecentOwnershipNrIngestions/types';
import ViewBulkUploads from '../viewBulkUploads';

describe('<ViewBulkUploads>', () => {
    const renderWrapper = () => renderInAppContext(<ViewBulkUploads />);
    const mockQueries = (data: NrOwnershipIngestionFile[]) => {
        jest.spyOn(
            queries,
            'useGetRecentOwnershipIngestionsFileProgress'
        ).mockReturnValue({
            data,
            error: undefined,
            stopPolling: jest.fn(),
            startPolling: jest.fn(),
        });
    };

    beforeEach(() => {
        mockQueries([]);
    });

    test('renders ViewBulkUploads button', () => {
        const { getByText } = renderWrapper();
        expect(
            getByText($t('neighbouringRights.viewBulkUploads.bulkUploads'))
        ).toBeVisible();
    });

    describe('on click ViewBulkUploads button', () => {
        test('it shows active and completed uploads', async () => {
            const data = [
                {
                    lastModified: '2022-03-03T09:40:14.000Z',
                    originalFilename: 'killjoys.csv',
                    id: 'ddb77517-0012-4ebe-b5eb-bf03e461d3ab.csv',
                    identity: {
                        id: 'some-id',
                        firstName: 'Allan',
                        lastName: 'Moon',
                    },
                    ingestionCompleted: true,
                    errorFileUrl: '',
                    instancesCreatedUpdated: 1,
                    failedIsrcCount: 0,
                },
                {
                    lastModified: '2022-03-03T05:02:13.000Z',
                    originalFilename: 'star_trek.csv',
                    id: '6a5ac6a5-7749-446d-a483-677449051689.csv',
                    identity: {
                        id: 'some-id',
                        firstName: 'Allan',
                        lastName: 'Moon',
                    },
                    ingestionCompleted: true,
                    errorFileUrl:
                        'https://qa-nr/ingestion/6a5ac6a5-7749-446d-a483-677449051689/error.csv',
                    instancesCreatedUpdated: 2,
                    failedIsrcCount: 1,
                },
                {
                    lastModified: '2022-03-01T13:25:46.000Z',
                    originalFilename: 'Doctor-Who.csv',
                    id: '8060aa56-5838-4791-b558-d12ca5c5e095.csv',
                    identity: {
                        id: 'some-id',
                        firstName: 'Allan',
                        lastName: 'Moon',
                    },
                    ingestionCompleted: false,
                    errorFileUrl:
                        'https://qa-nr/ingestion/8060aa56-5838-4791-b558-d12ca5c5e095/error.csv',
                    instancesCreatedUpdated: 3,
                    failedIsrcCount: 1,
                },
            ];
            mockQueries(data);
            const { getByText, findByTestId } = renderWrapper();
            const viewButton = getByText(
                $t('neighbouringRights.viewBulkUploads.bulkUploads')
            );
            expect(viewButton).toBeTruthy();

            fireEvent.click(viewButton);
            const fileOne = await findByTestId(
                'ddb77517-0012-4ebe-b5eb-bf03e461d3ab.csv'
            );
            const fileTwo = await findByTestId(
                '6a5ac6a5-7749-446d-a483-677449051689.csv'
            );
            const fileThree = await findByTestId(
                '8060aa56-5838-4791-b558-d12ca5c5e095.csv'
            );

            expect(fileOne).toBeTruthy();
            expect(fileTwo).toBeTruthy();
            expect(fileThree).toBeTruthy();
            expect(getByText(/killjoys\.csv/)).toBeTruthy();
            expect(getByText(/star_trek\.csv/)).toBeTruthy();
        });
    });

    describe('on clicking Close button', () => {
        const segmentTrack = jest.spyOn(Segment, 'trackEvent');
        test('tracks event', () => {
            const { getByText, getAllByRole } = renderWrapper();
            const viewButton = getByText(
                $t('neighbouringRights.viewBulkUploads.bulkUploads')
            );
            fireEvent.click(viewButton);
            const closeButton = getAllByRole('button')[1];
            if (closeButton) fireEvent.click(closeButton);
            expect(segmentTrack).toHaveBeenLastCalledWith(
                'Click',
                { category: 'Content Sound Recording Ownership' },
                'Close Ownership Bulk Uploads'
            );
        });
    });
});
