import React from 'react';
import { render, screen } from '@testing-library/react';
import { ContractTermException } from 'src/types/contract-term';
import { RenderExceptions } from '../contract-term-details-presentational-group-exceptions';

const setup = (
    exceptions: ContractTermException[],
    showExceptions: boolean = true
) => {
    return render(
        <RenderExceptions
            exceptions={exceptions}
            showExceptions={showExceptions}
        />
    );
};

describe('<RenderExceptions />', () => {
    it('does not render when showExceptions is false', () => {
        setup([], false);
        expect(
            screen.queryByTestId('ContractTermDetailsExceptions')
        ).not.toBeInTheDocument();
    });

    it('does not render when exceptions array is empty', () => {
        setup([]);
        expect(
            screen.queryByTestId('ContractTermDetailsExceptions')
        ).not.toBeInTheDocument();
    });

    it('does not render when all exceptions are "All" for both country and service', () => {
        const exceptions: ContractTermException[] = [
            { country: ['All'], service: ['All'], labelShare: 50 },
        ];
        setup(exceptions);
        expect(
            screen.queryByTestId('ContractTermDetailsExceptions')
        ).not.toBeInTheDocument();
    });

    it('renders single exception correctly', () => {
        const exceptions: ContractTermException[] = [
            { country: ['US'], service: ['Streaming'], labelShare: 12 },
        ];
        setup(exceptions);
        const exceptionContainer = screen.getByTestId(
            'ContractTermDetailsExceptions'
        );
        expect(exceptionContainer).toHaveTextContent(
            'Except 12% Label’s Share for Streaming in US'
        );
    });

    it('renders multiple exceptions correctly', () => {
        const exceptions: ContractTermException[] = [
            {
                country: ['CA'],
                service: ['Spotify', 'Apple Music', 'YouTube'],
                labelShare: 20,
            },
            { country: ['UK'], service: ['All'], labelShare: 25 },
        ];
        setup(exceptions);
        const exceptionContainer = screen.getByTestId(
            'ContractTermDetailsExceptions'
        );
        expect(exceptionContainer).toHaveTextContent(
            'Except 20% Label’s Share for Spotify, Apple Music, YouTube in CA'
        );
        expect(exceptionContainer).toHaveTextContent(
            'Except 25% Label’s Share in UK'
        );
    });

    it('correctly formats "All" services and countries', () => {
        const exceptions: ContractTermException[] = [
            {
                country: ['All'],
                service: ['Spotify', 'Apple Music', 'YouTube', 'Amazon Music'],
                labelShare: 30,
            },
            {
                country: ['US', 'GB', 'CA', 'FR'],
                service: ['All'],
                labelShare: 40,
            },
        ];
        setup(exceptions);
        const exceptionContainer = screen.getByTestId(
            'ContractTermDetailsExceptions'
        );
        expect(exceptionContainer).toHaveTextContent(
            'Except 30% Label’s Share for Spotify, Apple Music, YouTube + 1 additional services'
        );
        expect(exceptionContainer).toHaveTextContent(
            'Except 40% Label’s Share in US, GB, CA + 1 additional countries'
        );
    });

    it('correctly handles missing services or countries', () => {
        const exceptions: ContractTermException[] = [
            { country: ['All'], service: ['All'], labelShare: 50 }, // Should not be rendered
            { country: ['DE'], service: ['All'], labelShare: 55 },
            { country: ['All'], service: ['Pandora'], labelShare: 60 },
        ];
        setup(exceptions);
        const exceptionContainer = screen.getByTestId(
            'ContractTermDetailsExceptions'
        );
        expect(exceptionContainer).toHaveTextContent(
            'Except 55% Label’s Share in DE'
        );
        expect(exceptionContainer).toHaveTextContent(
            'Except 60% Label’s Share for Pandora'
        );
        expect(exceptionContainer).not.toHaveTextContent(
            'Except 50% Label’s Share'
        );
    });
});
