import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
jest.mock('next-intl', () => ({
  useTranslations: () => (key: string) => key,
  useLocale: () => 'fr',
}));
import ParticipantSection from '../ParticipantSection';
import { getParticipations } from '@/services/participationService';

jest.mock('@/services/participationService');
jest.mock('next/link', () => {
  return ({ children, href, ...props }: { children: React.ReactNode; href: string; [key: string]: unknown }) => (
    <a href={href} {...props}>{children}</a>
  );
});

const mockedGetParticipations = getParticipations as jest.MockedFunction<typeof getParticipations>;

describe('ParticipantSection', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('shouldLinkParticipantNameToPublicProfile', async () => {
    mockedGetParticipations.mockResolvedValue({
      count: 2,
      participants: [
        { id: 1, pseudo: 'alice29', userId: 42 },
        { id: 2, pseudo: 'bob56', userId: 99 },
      ],
    });

    render(<ParticipantSection evenementId={10} />);

    await waitFor(() => {
      expect(screen.getByText('alice29')).toBeInTheDocument();
    });

    const aliceLink = screen.getByText('alice29').closest('a');
    expect(aliceLink).toHaveAttribute('href', '/fr/profil/42');

    const bobLink = screen.getByText('bob56').closest('a');
    expect(bobLink).toHaveAttribute('href', '/fr/profil/99');
  });

  it('shouldSayWhenNobodyIsRegisteredYet', async () => {
    mockedGetParticipations.mockResolvedValue({count: 0, participants: []});
    render(<ParticipantSection evenementId={10} />);

    await waitFor(() => {
      expect(screen.getByText('empty')).toBeInTheDocument();
    });
    expect(screen.getByText('participants_title')).toBeInTheDocument();
  });
});
