/* eslint-disable react/prop-types */
/* eslint-disable react/destructuring-assignment */
import React from 'react';
import { render, screen } from '@testing-library/react';
import StickyOperatorBanner from '.';

// Mock dependencies
jest.mock('~helpers/getters', () => ({
  imagePrettyUrl: jest.fn((url) => `pretty/${url}`),
  removeDomain: jest.fn((url) => url), // MOCK removeDomain
}));

jest.mock('~hooks/lazy-image', () => (props) => (
  <img data-testid="lazy-image" {...props} alt="Test" />
));
jest.mock('~atoms/pretty-link', () => (props) => (
  <a data-testid="pretty-link" {...props}>
    {props.children}
  </a>
));

describe('StickyOperatorBanner', () => {
  const mockData = {
    modules: [
      {
        name: 'top_list',
        items: [
          {
            tracker: 'test-tracker',
            items: [
              { id: 1, name: 'Operator A', extra_fields: { gif: { url: 'banner.gif' } } },
              { id: 2, name: 'Operator B', extra_fields: { gif: { url: 'banner2.gif' } } },
            ],
          },
        ],
      },
    ],
  };

  // Always pick first item for deterministic test
  beforeAll(() => {
    jest.spyOn(Math, 'random').mockReturnValue(0);
  });

  afterAll(() => {
    jest.spyOn(Math, 'random').mockRestore();
  });

  it('renders banner with LazyImage and PrettyLink', () => {
    render(<StickyOperatorBanner data={mockData} template="casino" />);

    const image = screen.getByTestId('lazy-image');
    const link = screen.getByTestId('pretty-link');

    expect(image).toBeInTheDocument();
    expect(image).toHaveAttribute('src', 'pretty/banner.gif'); // now deterministic
    expect(link).toBeInTheDocument();
  });

  it('renders null if gif is missing in randomItem', () => {
    const dataWithoutGif = {
      modules: [
        {
          name: 'top_list',
          items: [{ items: [{ id: 1, name: 'Operator A', extra_fields: {} }] }],
        },
      ],
    };
    const { container } = render(<StickyOperatorBanner data={dataWithoutGif} />);
    expect(container.firstChild).toBeNull();
  });

  it('renders null if toplist items missing', () => {
    const dataWithoutItems = { modules: [] };
    const { container } = render(<StickyOperatorBanner data={dataWithoutItems} />);
    expect(container.firstChild).toBeNull();
  });

  it('uses default tracker if not provided', () => {
    const dataNoTracker = {
      modules: [
        {
          name: 'top_list',
          items: [
            {
              items: [{ id: 1, name: 'Operator X', extra_fields: { gif: { url: 'banner.gif' } } }],
            },
          ],
        },
      ],
    };

    render(<StickyOperatorBanner data={dataNoTracker} />);
    const link = screen.getByTestId('pretty-link');
    expect(link).toHaveAttribute('tracker', 'main');
  });
});
