import React from 'react';
import { render } from '@testing-library/react-native';
import {
  WiFiSignalIndicator,
  getSignalStrengthLabel,
  getSignalStrengthColor,
} from '@/components/WiFiSignalIndicator';
import { AppColors } from '@/constants/theme';

describe('WiFiSignalIndicator', () => {
  describe('Component rendering', () => {
    it('renders without crashing', () => {
      const { root } = render(<WiFiSignalIndicator rssi={-50} />);
      expect(root).toBeDefined();
    });

    it('renders with small size', () => {
      const { root } = render(<WiFiSignalIndicator rssi={-50} size="small" />);
      expect(root).toBeDefined();
    });

    it('renders with medium size', () => {
      const { root } = render(<WiFiSignalIndicator rssi={-50} size="medium" />);
      expect(root).toBeDefined();
    });

    it('renders with large size', () => {
      const { root } = render(<WiFiSignalIndicator rssi={-50} size="large" />);
      expect(root).toBeDefined();
    });
  });

  describe('Signal strength logic', () => {
    it('shows excellent signal for RSSI >= -50', () => {
      expect(getSignalStrengthLabel(-50)).toBe('Excellent');
      expect(getSignalStrengthLabel(-40)).toBe('Excellent');
      expect(getSignalStrengthLabel(-30)).toBe('Excellent');
    });

    it('shows good signal for RSSI >= -60 and < -50', () => {
      expect(getSignalStrengthLabel(-60)).toBe('Good');
      expect(getSignalStrengthLabel(-55)).toBe('Good');
      expect(getSignalStrengthLabel(-59)).toBe('Good');
    });

    it('shows fair signal for RSSI >= -70 and < -60', () => {
      expect(getSignalStrengthLabel(-70)).toBe('Fair');
      expect(getSignalStrengthLabel(-65)).toBe('Fair');
      expect(getSignalStrengthLabel(-69)).toBe('Fair');
    });

    it('shows weak signal for RSSI < -70', () => {
      expect(getSignalStrengthLabel(-71)).toBe('Weak');
      expect(getSignalStrengthLabel(-80)).toBe('Weak');
      expect(getSignalStrengthLabel(-90)).toBe('Weak');
    });
  });

  describe('Signal strength colors', () => {
    it('returns success color for excellent signal', () => {
      expect(getSignalStrengthColor(-50)).toBe(AppColors.success);
      expect(getSignalStrengthColor(-40)).toBe(AppColors.success);
    });

    it('returns info color for good signal', () => {
      expect(getSignalStrengthColor(-60)).toBe(AppColors.info);
      expect(getSignalStrengthColor(-55)).toBe(AppColors.info);
    });

    it('returns warning color for fair signal', () => {
      expect(getSignalStrengthColor(-70)).toBe(AppColors.warning);
      expect(getSignalStrengthColor(-65)).toBe(AppColors.warning);
    });

    it('returns error color for weak signal', () => {
      expect(getSignalStrengthColor(-71)).toBe(AppColors.error);
      expect(getSignalStrengthColor(-80)).toBe(AppColors.error);
    });
  });

  describe('Edge cases', () => {
    it('handles boundary values correctly', () => {
      // Test exact boundary values
      expect(getSignalStrengthLabel(-50)).toBe('Excellent'); // Boundary: excellent/good
      expect(getSignalStrengthLabel(-51)).toBe('Good');

      expect(getSignalStrengthLabel(-60)).toBe('Good'); // Boundary: good/fair
      expect(getSignalStrengthLabel(-61)).toBe('Fair');

      expect(getSignalStrengthLabel(-70)).toBe('Fair'); // Boundary: fair/weak
      expect(getSignalStrengthLabel(-71)).toBe('Weak');
    });

    it('handles extreme RSSI values', () => {
      expect(getSignalStrengthLabel(-10)).toBe('Excellent'); // Very strong signal
      expect(getSignalStrengthLabel(-100)).toBe('Weak'); // Very weak signal
      expect(getSignalStrengthLabel(0)).toBe('Excellent'); // Maximum possible
    });

    it('handles negative zero', () => {
      expect(getSignalStrengthLabel(-0)).toBe('Excellent');
    });
  });

  describe('Real-world RSSI values', () => {
    it('handles typical home WiFi signals', () => {
      expect(getSignalStrengthLabel(-45)).toBe('Excellent'); // Next to router
      expect(getSignalStrengthLabel(-55)).toBe('Good'); // Same room
      expect(getSignalStrengthLabel(-65)).toBe('Fair'); // Next room
      expect(getSignalStrengthLabel(-75)).toBe('Weak'); // Far away
    });

    it('handles typical office WiFi signals', () => {
      expect(getSignalStrengthLabel(-50)).toBe('Excellent');
      expect(getSignalStrengthLabel(-60)).toBe('Good');
      expect(getSignalStrengthLabel(-70)).toBe('Fair');
      expect(getSignalStrengthLabel(-80)).toBe('Weak');
    });
  });

  describe('Component props', () => {
    it('accepts valid RSSI values', () => {
      const rssiValues = [-40, -50, -60, -70, -80];
      rssiValues.forEach((rssi) => {
        const { root } = render(<WiFiSignalIndicator rssi={rssi} />);
        expect(root).toBeDefined();
      });
    });

    it('accepts all size variants', () => {
      const sizes: ('small' | 'medium' | 'large')[] = ['small', 'medium', 'large'];
      sizes.forEach((size) => {
        const { root } = render(<WiFiSignalIndicator rssi={-50} size={size} />);
        expect(root).toBeDefined();
      });
    });

    it('uses medium size by default', () => {
      const withoutSize = render(<WiFiSignalIndicator rssi={-50} />);
      const withMediumSize = render(<WiFiSignalIndicator rssi={-50} size="medium" />);
      expect(withoutSize.root).toBeDefined();
      expect(withMediumSize.root).toBeDefined();
    });
  });
});
