import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import FloatingHelpButton from '../FloatingHelpButton';

// Mock react-i18next
vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    t: (key: string, defaultValue?: string) => defaultValue || key,
  }),
}));

describe('FloatingHelpButton', () => {
  const renderWithRouter = (initialPath: string) => {
    return render(
      <MemoryRouter initialEntries={[initialPath]}>
        <FloatingHelpButton />
      </MemoryRouter>
    );
  };

  it('renders help link on dashboard', () => {
    renderWithRouter('/dashboard');
    const link = screen.getByRole('link');
    expect(link).toBeInTheDocument();
  });

  it('links to correct help page for dashboard', () => {
    renderWithRouter('/dashboard');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('href', '/help/dashboard');
  });

  it('links to correct help page for scheduler', () => {
    renderWithRouter('/scheduler');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('href', '/help/scheduler');
  });

  it('links to correct help page for services', () => {
    renderWithRouter('/services');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('href', '/help/services');
  });

  it('links to correct help page for resources', () => {
    renderWithRouter('/resources');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('href', '/help/resources');
  });

  it('links to correct help page for settings', () => {
    renderWithRouter('/settings/general');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('href', '/help/settings/general');
  });

  it('returns null on help pages', () => {
    const { container } = renderWithRouter('/help/dashboard');
    expect(container.firstChild).toBeNull();
  });

  it('has aria-label', () => {
    renderWithRouter('/dashboard');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('aria-label', 'Help');
  });

  it('has title attribute', () => {
    renderWithRouter('/dashboard');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('title', 'Help');
  });

  it('links to default help for unknown routes', () => {
    renderWithRouter('/unknown-route');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('href', '/help');
  });

  it('handles dynamic routes by matching prefix', () => {
    renderWithRouter('/customers/123');
    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('href', '/help/customers');
  });
});
