import React from 'react';
import {render, screen} from '@testing-library/react';

jest.mock('next-intl', () => ({
  useTranslations: () => (key: string) => key,
}));
jest.mock('next/image', () => ({
  __esModule: true,
  default: ({src, alt}: {src: string; alt: string}) => <img src={src} alt={alt} />,
}));
jest.mock('@/services/epciService', () => ({
  getEpciLogo: jest.fn(),
}));

import EpciLogoDisplay from '../EpciLogoDisplay';
import {getEpciLogo} from '@/services/epciService';

const mockedGetEpciLogo = getEpciLogo as jest.Mock;

const qbo = {
  suaId: 3,
  suaNom: 'Quimper Bretagne Occidentale',
  suaPhrasePartenaire: 'Soutenu par notre intercommunalité',
  suaUrl: 'https://www.qbo.bzh',
};

describe('EpciLogoDisplay', () => {
  beforeEach(() => {
    jest.clearAllMocks();
    mockedGetEpciLogo.mockResolvedValue('https://media.tamm-kreiz.bzh/qbo.png');
  });

  it('shouldLinkToThePartnerTerritoriesArticleWhenConfigured', async () => {
    render(<EpciLogoDisplay {...qbo} suaArticlePartenaire="https://www.tamm-kreiz.bzh/mag/territoires" />);

    const link = await screen.findByRole('link', {name: 'epci_more_link'});
    expect(link).toHaveAttribute('href', 'https://www.tamm-kreiz.bzh/mag/territoires');
  });

  it('shouldKeepTheLogoLinkedToTheEpciWebsite', async () => {
    render(<EpciLogoDisplay {...qbo} suaArticlePartenaire="https://www.tamm-kreiz.bzh/mag/territoires" />);

    const logo = await screen.findByRole('img', {name: 'Logo Quimper Bretagne Occidentale'});
    expect(logo.closest('a')).toHaveAttribute('href', 'https://www.qbo.bzh');
  });

  it('shouldNotOfferTheArticleLinkWithoutUrl', async () => {
    render(<EpciLogoDisplay {...qbo} />);

    await screen.findByRole('img');
    expect(screen.queryByRole('link', {name: 'epci_more_link'})).not.toBeInTheDocument();
  });
});
