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

jest.mock('next-intl', () => ({
  useTranslations: () => (key: string) => key,
}));
jest.mock('next/link', () => ({
  __esModule: true,
  default: ({children, href}: {children: React.ReactNode; href: string}) => <a href={href}>{children}</a>,
}));
jest.mock('@/services/communauteService', () => ({
  getFilCommunautaire: jest.fn(),
}));

import CommunauteFeed from '../CommunauteFeed';
import {getFilCommunautaire} from '@/services/communauteService';

const evenement = {eveId: 72, libelle: 'Fest-noz à Rennes', date: '2026-09-20', tevLibelle: 'Fest-noz', vilNom: 'Rennes'};

describe('CommunauteFeed', () => {
  it('shouldShowTheFourLatestListsWithLinksToTheEvents', async () => {
    (getFilCommunautaire as jest.Mock).mockResolvedValue({
      commentaires: [{id: 1, texte: 'Super soirée', date: '2026-09-17', auteur: {id: 1, pseudo: 'breizh56'}, evenement}],
      annonces: [{id: 2, titre: 'Cherche sonneur', date: '2026-09-16', typeId: 1, auteur: {id: 1, pseudo: 'breizh56'}}],
      covoiturages: [{id: 3, ville: 'Brest', nombre: 2, propose: 1, auteur: {id: 2, pseudo: 'yann22'}, evenement}],
      inscrits: [{id: 4, pseudo: 'alice29', userId: 42, evenement}],
    });

    render(<CommunauteFeed locale="fr" />);

    expect(await screen.findByText('Super soirée')).toBeInTheDocument();
    expect(screen.getByText('Cherche sonneur')).toBeInTheDocument();
    expect(screen.getByText(/Brest/)).toBeInTheDocument();
    expect(screen.getByText('alice29')).toBeInTheDocument();
    expect(screen.getAllByRole('link', {name: /Fest-noz à Rennes/})[0]).toHaveAttribute('href', '/fr/evenement/72/fest-noz-a-rennes');
    expect(screen.getByText('latest_comments')).toBeInTheDocument();
  });

  it('shouldRenderNothingWhenEverythingIsEmpty', async () => {
    (getFilCommunautaire as jest.Mock).mockResolvedValue({commentaires: [], annonces: [], covoiturages: [], inscrits: []});

    const {container} = render(<CommunauteFeed locale="fr" />);

    await screen.findByTestId('communaute-feed-vide');
    expect(container.querySelector('[data-testid="communaute-feed"]')).toBeNull();
  });
});
