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

describe('NavigationTabs', () => {
  it('opens the mobile menu with account actions', () => {
    const onLogout = jest.fn();
    render(<NavigationTabs profileName="Meik" onLogout={onLogout} />);

    fireEvent.click(screen.getByRole('button', { name: 'Menü öffnen' }));

    expect(screen.getByText('Meik')).toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: 'Logout' }));
    expect(onLogout).toHaveBeenCalledTimes(1);
  });

  it('uses the existing protected navigation callback', () => {
    const onProtectedNavigate = jest.fn();
    render(<NavigationTabs isAdmin onProtectedNavigate={onProtectedNavigate} />);

    fireEvent.click(screen.getByRole('button', { name: 'Menü öffnen' }));
    fireEvent.click(screen.getAllByText('Betriebs-Monitoring')[0]);

    expect(onProtectedNavigate).toHaveBeenCalledWith(
      'zur Seite "Betriebs-Monitoring" zu wechseln',
      expect.any(Function)
    );
  });

  it('runs slot tools from the menu and closes it afterwards', () => {
    const onRefresh = jest.fn();
    const onToggleNotifications = jest.fn();
    render(<NavigationTabs onRefresh={onRefresh} onToggleNotifications={onToggleNotifications} />);

    fireEvent.click(screen.getByRole('button', { name: 'Menü öffnen' }));
    fireEvent.click(screen.getByRole('button', { name: 'Betriebe aktualisieren' }));

    expect(onRefresh).toHaveBeenCalledTimes(1);
    expect(screen.queryByText('Werkzeuge')).not.toBeInTheDocument();

    fireEvent.click(screen.getByRole('button', { name: 'Menü öffnen' }));
    fireEvent.click(screen.getByRole('button', { name: 'Benachrichtigungen' }));
    expect(onToggleNotifications).toHaveBeenCalledTimes(1);
  });

  it('hides admin-only sections and the desktop navigation for regular users', () => {
    render(<NavigationTabs />);

    expect(screen.queryByText('Betriebs-Monitoring')).not.toBeInTheDocument();
    expect(screen.queryByText('Abhol-Journal')).not.toBeInTheDocument();
    expect(screen.queryByText('Slots buchen')).not.toBeInTheDocument();
  });
});
