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

vi.mock('next/navigation', () => ({
  usePathname: () => '/',
}));

vi.mock('next/link', () => {
  const ActualLink = ({ href, children, ...rest }: any) => (
    <a href={href} {...rest}>
      {children}
    </a>
  );
  return { default: ActualLink };
});

import { AlphaNav } from '../../../apps/website/components/alpha/AlphaNav';

describe('AlphaNav', () => {
  it('hides Dashboard link and shows login when unauthenticated', () => {
    render(<AlphaNav isAuthenticated={false} />);

    const dashboardLinks = screen.queryAllByText('Dashboard');
    expect(dashboardLinks.length).toBe(0);

    const homeLink = screen.getByText('Home');
    expect(homeLink).toBeInTheDocument();

    const login = screen.getByText('Authenticate with iRacing');
    expect(login).toBeInTheDocument();
    expect((login as HTMLAnchorElement).getAttribute('href')).toContain(
      '/auth/iracing/start?returnTo=/dashboard',
    );
  });

  it('shows Dashboard link, hides Home, and logout control when authenticated', () => {
    render(<AlphaNav isAuthenticated />);

    const dashboard = screen.getByText('Dashboard');
    expect(dashboard).toBeInTheDocument();
    expect((dashboard as HTMLAnchorElement).getAttribute('href')).toBe('/dashboard');

    const homeLink = screen.queryByText('Home');
    expect(homeLink).toBeNull();

    const login = screen.queryByText('Authenticate with iRacing');
    expect(login).toBeNull();

    const logout = screen.getByText('Logout');
    expect(logout).toBeInTheDocument();
  });
});