import { ThemeProvider } from '@wise/components-theming';

import { render, screen } from '../test-utils';
import SentimentSurface from './SentimentSurface';
import { Sentiment, Emphasis } from './SentimentSurface.types';

describe('SentimentSurface', () => {
  const testContent = 'Test content';

  describe('rendering', () => {
    it('renders with children', () => {
      render(<SentimentSurface sentiment="neutral">{testContent}</SentimentSurface>);
      expect(screen.getByText(testContent)).toBeInTheDocument();
    });

    it('renders as div by default', () => {
      render(
        <SentimentSurface sentiment="neutral" data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId('surface').tagName).toBe('DIV');
    });

    it('renders as custom element when as prop is provided', () => {
      render(
        <SentimentSurface sentiment="neutral" as="section" data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId('surface').tagName).toBe('SECTION');
    });
  });

  describe('hasBaseStyles', () => {
    it('applies base styles by default', () => {
      render(
        <SentimentSurface sentiment="neutral" data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId('surface')).toHaveClass('wds-sentiment-surface--hasBaseStyles');
    });

    it('does not apply base styles when hasBaseStyles is false', () => {
      render(
        <SentimentSurface sentiment="neutral" hasBaseStyles={false} data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId('surface')).not.toHaveClass('wds-sentiment-surface--hasBaseStyles');
    });
  });

  describe('sentiment variants', () => {
    const sentiments: Sentiment[] = ['negative', 'warning', 'neutral', 'success', 'proposition'];

    sentiments.forEach((sentiment) => {
      it(`renders ${sentiment} sentiment with correct class`, () => {
        render(
          <SentimentSurface sentiment={sentiment} data-testid="surface">
            {testContent}
          </SentimentSurface>,
        );
        const element = screen.getByTestId('surface');
        expect(element).toHaveClass('wds-sentiment-surface');
        expect(element).toHaveClass(`wds-sentiment-${sentiment}-light-base`);
      });
    });
  });

  describe('emphasis variants', () => {
    const emphasisLevels: Emphasis[] = ['base', 'elevated'];

    emphasisLevels.forEach((emphasis) => {
      it(`renders ${emphasis} emphasis with correct class`, () => {
        render(
          <SentimentSurface sentiment="neutral" emphasis={emphasis} data-testid="surface">
            {testContent}
          </SentimentSurface>,
        );
        const element = screen.getByTestId('surface');
        expect(element).toHaveClass('wds-sentiment-surface');
        expect(element).toHaveClass(`wds-sentiment-neutral-light-${emphasis}`);
      });
    });

    it('defaults to base emphasis when not provided', () => {
      render(
        <SentimentSurface sentiment="neutral" data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      const element = screen.getByTestId('surface');
      expect(element).toHaveClass('wds-sentiment-surface');
      expect(element).toHaveClass('wds-sentiment-neutral-light-base');
    });
  });

  describe('custom props', () => {
    it('applies custom className', () => {
      const customClass = 'custom-class';
      render(
        <SentimentSurface sentiment="neutral" className={customClass} data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId('surface')).toHaveClass(customClass);
    });

    it('applies custom id', () => {
      const customId = 'custom-id';
      render(
        <SentimentSurface sentiment="neutral" id={customId}>
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByText(testContent)).toHaveAttribute('id', customId);
    });

    it('applies data-testid attribute', () => {
      const testId = 'test-surface';
      render(
        <SentimentSurface sentiment="neutral" data-testid={testId}>
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId(testId)).toBeInTheDocument();
    });

    it('applies custom style', () => {
      const customStyle = { padding: '20px' };
      render(
        <SentimentSurface sentiment="neutral" style={customStyle} data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId('surface')).toHaveStyle(customStyle);
    });
  });

  describe('dark mode detection', () => {
    it('uses dark mode class for forest-green theme', () => {
      render(
        <ThemeProvider theme="forest-green">
          <SentimentSurface sentiment="negative" data-testid="surface">
            {testContent}
          </SentimentSurface>
        </ThemeProvider>,
      );
      expect(screen.getByTestId('surface')).toHaveClass('wds-sentiment-negative-dark-base');
    });

    it('uses dark mode class for business--forest-green theme', () => {
      render(
        <ThemeProvider theme="business--forest-green">
          <SentimentSurface sentiment="warning" emphasis="elevated" data-testid="surface">
            {testContent}
          </SentimentSurface>
        </ThemeProvider>,
      );
      expect(screen.getByTestId('surface')).toHaveClass('wds-sentiment-warning-dark-elevated');
    });

    it('uses dark mode class for dark screen mode', () => {
      render(
        <ThemeProvider theme="personal" screenMode="dark">
          <SentimentSurface sentiment="success" data-testid="surface">
            {testContent}
          </SentimentSurface>
        </ThemeProvider>,
      );
      expect(screen.getByTestId('surface')).toHaveClass('wds-sentiment-success-dark-base');
    });

    it('uses light mode class for bright-green theme', () => {
      render(
        <ThemeProvider theme="bright-green">
          <SentimentSurface sentiment="neutral" data-testid="surface">
            {testContent}
          </SentimentSurface>
        </ThemeProvider>,
      );
      expect(screen.getByTestId('surface')).toHaveClass('wds-sentiment-neutral-light-base');
    });

    it('uses light mode class for np-theme-platform in light mode', () => {
      render(
        <ThemeProvider theme="platform" screenMode="light">
          <SentimentSurface sentiment="neutral" data-testid="surface">
            {testContent}
          </SentimentSurface>
        </ThemeProvider>,
      );
      expect(screen.getByTestId('surface')).toHaveClass('wds-sentiment-neutral-light-base');
    });

    it('uses light mode class for np-theme-platform in dark mode', () => {
      render(
        <ThemeProvider theme="platform" screenMode="dark">
          <SentimentSurface sentiment="neutral" data-testid="surface">
            {testContent}
          </SentimentSurface>
        </ThemeProvider>,
      );
      expect(screen.getByTestId('surface')).toHaveClass('wds-sentiment-neutral-light-base');
    });

    it('uses dark mode class for np-theme-platform--forest-green', () => {
      render(
        <ThemeProvider theme="platform--forest-green">
          <SentimentSurface sentiment="negative" data-testid="surface">
            {testContent}
          </SentimentSurface>
        </ThemeProvider>,
      );
      expect(screen.getByTestId('surface')).toHaveClass('wds-sentiment-negative-dark-base');
    });
  });

  describe('HTML attributes', () => {
    it('supports aria-label attribute', () => {
      const ariaLabel = 'Surface label';
      render(
        <SentimentSurface sentiment="neutral" aria-label={ariaLabel} data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId('surface')).toHaveAttribute('aria-label', ariaLabel);
    });

    it('supports role attribute', () => {
      render(
        <SentimentSurface sentiment="neutral" role="region" data-testid="surface">
          {testContent}
        </SentimentSurface>,
      );
      expect(screen.getByTestId('surface')).toHaveAttribute('role', 'region');
    });
  });
});
