import { bambuLabColors, getFilamentColor, getColorStyle, ColorMapping } from '@/src/data/bambuLabColors';

describe('Bambu Lab Colors Data', () => {
  describe('bambuLabColors', () => {
    it('should have color definitions', () => {
      expect(Object.keys(bambuLabColors).length).toBeGreaterThan(0);
    });

    it('should have correct structure for each color', () => {
      Object.entries(bambuLabColors).forEach(([colorName, colorMapping]) => {
        expect(colorMapping).toHaveProperty('hex');
        expect(colorMapping.hex).toBeDefined();
      });
    });

    it('should have known colors', () => {
      expect(bambuLabColors).toHaveProperty('Black');
      expect(bambuLabColors).toHaveProperty('White');
      expect(bambuLabColors).toHaveProperty('Red');
      expect(bambuLabColors).toHaveProperty('Blue');
      expect(bambuLabColors).toHaveProperty('Green');
    });

    it('should have valid hex colors', () => {
      Object.entries(bambuLabColors).forEach(([colorName, colorMapping]) => {
        if (typeof colorMapping.hex === 'string') {
          expect(colorMapping.hex).toMatch(/^#[0-9A-Fa-f]{6}$/);
        } else if (Array.isArray(colorMapping.hex)) {
          colorMapping.hex.forEach(hex => {
            expect(hex).toMatch(/^#[0-9A-Fa-f]{6}$/);
          });
        }
      });
    });

    it('should have Unknown fallback color', () => {
      expect(bambuLabColors).toHaveProperty('Unknown');
      expect(bambuLabColors.Unknown.hex).toBe('#CCCCCC');
    });
  });

  describe('getFilamentColor', () => {
    it('should return exact match', () => {
      const result = getFilamentColor('Black');
      expect(result).toEqual(bambuLabColors['Black']);
    });

    it('should return case-insensitive match', () => {
      const result = getFilamentColor('black');
      expect(result).toEqual(bambuLabColors['Black']);
    });

    it('should return partial match', () => {
      const result = getFilamentColor('PLA Black');
      expect(result).toEqual(bambuLabColors['Black']);
    });

    it('should return default color for non-existent color', () => {
      const result = getFilamentColor('NonExistentColor');
      expect(result).toBeDefined();
      expect(result).toHaveProperty('hex');
    });

    it('should handle empty string', () => {
      const result = getFilamentColor('');
      expect(result).toBeDefined(); 
      expect(result).toHaveProperty('hex');
    });
  });

  describe('getColorStyle', () => {
    it('should return backgroundColor for single hex color', () => {
      const colorMapping: ColorMapping = { hex: '#FF0000' };
      const result = getColorStyle(colorMapping);
      expect(result).toEqual({ backgroundColor: '#FF0000' });
    });

    it('should return backgroundColor for hex array without gradient', () => {
      const colorMapping: ColorMapping = { hex: ['#FF0000', '#00FF00'] };
      const result = getColorStyle(colorMapping);
      expect(result).toEqual({ backgroundColor: '#FF0000' });
    });

    it('should return gradient for isGradient true', () => {
      const colorMapping: ColorMapping = { hex: ['#FF0000', '#00FF00'], isGradient: true };
      const result = getColorStyle(colorMapping);
      expect(result).toEqual({
        background: 'linear-gradient(90deg, #FF0000 0%, #00FF00 100%)'
      });
    });

    it('should handle gradient with more than 2 colors', () => {
      const colorMapping: ColorMapping = { hex: ['#FF0000', '#00FF00', '#0000FF'], isGradient: true };
      const result = getColorStyle(colorMapping);
      expect(result).toEqual({
        background: 'linear-gradient(90deg, #FF0000 0%, #00FF00 100%)'
      });
    });
  });
});