import { render, screen, fireEvent } from '@testing-library/react';
import { ThemeProvider } from '../../theme';

const push = jest.fn();
let mockRoles: string[] = [];

jest.mock('next-intl', () => ({
  useTranslations: () => (key: string) => key,
}));

jest.mock('@/i18n/navigation', () => ({
  useRouter: () => ({ push }),
}));

jest.mock('@/context/auth', () => ({
  useAuth: () => [{ currentUser: { roles: mockRoles } }],
}));

import ActorSwitcher from './ActorSwitcher';

describe('<ActorSwitcher/> component', () => {
  beforeEach(() => {
    push.mockClear();
    mockRoles = [];
  });

  it('renders nothing for a single-role session', () => {
    mockRoles = ['nurse'];
    const { container } = render(
      <ThemeProvider>
        <ActorSwitcher target="customer" />
      </ThemeProvider>,
    );
    expect(container).toBeEmptyDOMElement();
  });

  it('navigates to the nurse app for a dual-role session', () => {
    mockRoles = ['customer', 'nurse'];
    render(
      <ThemeProvider>
        <ActorSwitcher target="nurse" />
      </ThemeProvider>,
    );
    fireEvent.click(screen.getByText('switch_to_nurse'));
    expect(push).toHaveBeenCalledWith('/nurse');
  });

  it('navigates to the customer app for a dual-role session', () => {
    mockRoles = ['customer', 'nurse'];
    render(
      <ThemeProvider>
        <ActorSwitcher target="customer" />
      </ThemeProvider>,
    );
    fireEvent.click(screen.getByText('switch_to_customer'));
    expect(push).toHaveBeenCalledWith('/');
  });
});
