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

jest.mock('next-intl', () => ({
  NextIntlClientProvider: ({children}: {children: React.ReactNode}) => <>{children}</>,
  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('@/i18n', () => jest.fn().mockResolvedValue({messages: {DisciplineDetailsPage: {}}}));
jest.mock('@/services/disciplineService', () => ({
  getDiscipline: jest.fn(),
  getAnimateurs: jest.fn(),
  getFodLies: jest.fn(),
  getPratiquants: jest.fn().mockResolvedValue([]),
}));
jest.mock('@/components/shared/AnimateursGallery/AnimateursGallery', () => ({
  __esModule: true,
  default: ({animateurs, titleKey}: {animateurs?: {aenLibelle: string; photoUrl: string | null}[]; titleKey: string}) => (
    <div data-testid="animateurs" data-title={titleKey} data-count={animateurs?.length ?? 0} data-noms={(animateurs ?? []).map(a => a.aenLibelle).join('|')} />
  ),
}));
jest.mock('@/components/Disciplines/PratiquantsDiscipline', () => ({
  __esModule: true,
  default: ({instruments}: {instruments: {insNom: string}[]}) => <div data-testid="pratiquants" data-count={instruments.length} data-noms={instruments.map(i => i.insNom).join('|')} />,
}));
jest.mock('@/components/Formations/NextEventFormations/NextEventFormations', () => ({
  __esModule: true,
  default: ({fodId, fodNom}: {fodId: number; fodNom?: string}) => <div data-testid="next-formations" data-fod={fodId} data-nom={fodNom} />,
}));
jest.mock('@/components/community/FavoriteButton', () => ({__esModule: true, default: () => <div data-testid="favorite" />}));

import Page, {generateMetadata} from '../page';
import {getDiscipline, getAnimateurs, getFodLies} from '@/services/disciplineService';

const mockedGetDiscipline = getDiscipline as jest.Mock;
const mockedGetAnimateurs = getAnimateurs as jest.Mock;
const mockedGetFodLies = getFodLies as jest.Mock;

const props = {params: Promise.resolve({locale: 'fr', disId: '12'})};

describe('Page discipline — balise title', () => {
  beforeEach(() => {
    jest.clearAllMocks();
    mockedGetDiscipline.mockResolvedValue({fodId: 12, fodNom: 'Bombarde'});
    mockedGetAnimateurs.mockResolvedValue([]);
    mockedGetFodLies.mockResolvedValue([]);
  });

  it('shouldBuildTheDocumentTitleFromTheDisciplineName', async () => {
    expect(await generateMetadata(props)).toEqual({title: 'Bombarde - Tamm-Kreiz'});
  });

  it('shouldFallBackToTheSiteNameWhenTheDisciplineCannotBeLoaded', async () => {
    mockedGetDiscipline.mockRejectedValue(new Error('HTTP 404'));

    expect(await generateMetadata(props)).toEqual({title: 'Tamm-Kreiz'});
  });

  it('shouldStillRenderTheDisciplineHeading', async () => {
    render(await (Page as any)(props));

    expect(screen.getByRole('heading', {level: 1})).toHaveTextContent('Bombarde');
  });
});

describe('Page discipline — animateurs, artistes, liens', () => {
  beforeEach(() => {
    jest.clearAllMocks();
    mockedGetDiscipline.mockResolvedValue({fodId: 12, fodNom: 'Bombarde'});
    mockedGetAnimateurs.mockResolvedValue([]);
    mockedGetFodLies.mockResolvedValue([]);
  });

  it('shouldShowTheActiveTrainersGalleryAndTheInstrumentsBlock', async () => {
    const {getPratiquants} = jest.requireMock('@/services/disciplineService');
    mockedGetAnimateurs.mockResolvedValue([{aenId: 5, aenLibelle: 'Anna Le Goff', photoUrl: 'https://media.test/anna.jpg'}]);
    getPratiquants.mockResolvedValue([{insId: 3, insNom: 'Bombarde', pratiquants: []}]);

    render(await (Page as any)(props));

    expect(screen.getByTestId('animateurs')).toHaveAttribute('data-noms', 'Anna Le Goff');
    expect(screen.getByTestId('animateurs')).toHaveAttribute('data-title', 'animateurs_title');
    expect(screen.getByTestId('pratiquants')).toHaveAttribute('data-noms', 'Bombarde');
    expect(screen.getByTestId('next-formations')).toHaveAttribute('data-nom', 'Bombarde');
  });

  it('shouldLinkRelatedDisciplinesWithTheirSlug', async () => {
    mockedGetFodLies.mockResolvedValue([{fodId: 1, fodNom: 'Flûte traversière en bois'}]);

    render(await (Page as any)(props));

    expect(screen.getByRole('link', {name: 'Flûte traversière en bois'})).toHaveAttribute('href', '/fr/discipline/1/flute-traversiere-en-bois');
  });
});
