import { render, screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import { MemoryRouter } from 'react-router-dom';
import Sidebar from './Sidebar';
import { ThemeContext, type ThemeContextValue } from '@/contexts/ThemeContextDef';

function renderLightSidebar(initialEntries = ['/agent']) {
  const themeValue: ThemeContextValue = {
    theme: 'light',
    accent: 'blue',
    colorTheme: 'default-light',
    uiFont: 'system',
    monoFont: 'jetbrains',
    uiFontSize: 15,
    monoFontSize: 14,
    resolvedTheme: 'light',
    setTheme: () => {},
    setAccent: () => {},
    setColorTheme: () => {},
    setUiFont: () => {},
    setMonoFont: () => {},
    setUiFontSize: () => {},
    setMonoFontSize: () => {},
  };

  return render(
    <ThemeContext.Provider value={themeValue}>
      <MemoryRouter initialEntries={initialEntries}>
        <Sidebar open={false} onClose={() => {}} />
      </MemoryRouter>
    </ThemeContext.Provider>,
  );
}

describe('Sidebar', () => {
  it('shows a search input in the sidebar header', () => {
    renderLightSidebar(['/agent']);

    expect(screen.getByRole('searchbox', { name: 'Search...' })).toBeInTheDocument();
  });

  it('shows only the agent option on the agent route', () => {
    renderLightSidebar(['/agent']);

    expect(screen.getByRole('link', { name: 'Agent' })).toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Dashboard' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Configuration' })).not.toBeInTheDocument();
    expect(screen.getByTestId('app-sidebar')).toHaveAttribute('data-desktop-visible', 'false');
  });

  it('hides the agent option outside the agent route', () => {
    renderLightSidebar(['/config']);

    expect(screen.getByRole('link', { name: 'Dashboard' })).toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Configuration' })).toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Agent' })).not.toBeInTheDocument();
    expect(screen.getByTestId('app-sidebar')).toHaveAttribute('data-desktop-visible', 'true');
  });

  it('marks the light-theme sidebar for the reference window styling', () => {
    renderLightSidebar(['/agent']);

    expect(screen.getByTestId('app-sidebar')).toHaveAttribute('data-light-theme', 'true');
  });
});
