import { rankHybridEntries, scoreHybridEntry } from '../../utils/hybridSearch';

describe('hybridSearch', () => {
  const entries = [
    {
      name: 'Snake Plant',
      botanicalName: 'Sansevieria trifasciata',
      description: 'Very resilient houseplant that handles little light well.',
      categories: ['easy', 'low_light', 'air_purifier'],
      careInfo: { waterIntervalDays: 14, light: 'Low to full light', temp: '16-30C' },
    },
    {
      name: 'Spider Plant',
      botanicalName: 'Chlorophytum comosum',
      description: 'Easy houseplant that is safe for pets and helps clean indoor air.',
      categories: ['easy', 'pet_friendly', 'air_purifier'],
      careInfo: { waterIntervalDays: 6, light: 'Bright to partial shade', temp: '16-24C' },
    },
    {
      name: 'Anthurium',
      botanicalName: 'Anthurium andraeanum',
      description: 'Flowering tropical plant for bright indirect light.',
      categories: ['flowering'],
      careInfo: { waterIntervalDays: 6, light: 'Bright indirect light', temp: '18-27C' },
    },
    {
      name: 'Boston Fern',
      botanicalName: 'Nephrolepis exaltata',
      description: 'Loves steady moisture and humid rooms.',
      categories: ['high_humidity', 'hanging'],
      careInfo: { waterIntervalDays: 3, light: 'Partial shade', temp: '16-24C' },
    },
    {
      name: 'Aloe Vera',
      botanicalName: 'Aloe vera',
      description: 'Sun-loving succulent for bright windows.',
      categories: ['succulent', 'sun', 'medicinal'],
      careInfo: { waterIntervalDays: 12, light: 'Sunny', temp: '18-30C' },
    },
  ];

  it('ranks multi-intent matches above single-attribute matches', () => {
    const results = rankHybridEntries(entries, 'pet friendly air purifier', 3);
    expect(results[0].entry.name).toBe('Spider Plant');
  });

  it('understands natural low-light and easy-care intent', () => {
    const results = rankHybridEntries(entries, 'easy plant for dark corner', 3);
    expect(results[0].entry.name).toBe('Snake Plant');
  });

  it('keeps exact-name matches ahead of semantic-only matches', () => {
    const scores = entries.map((entry) => ({
      name: entry.name,
      score: scoreHybridEntry(entry, 'snake plant'),
    }));
    const snakeScore = scores.find((entry) => entry.name === 'Snake Plant')?.score || 0;
    const spiderScore = scores.find((entry) => entry.name === 'Spider Plant')?.score || 0;
    expect(snakeScore).toBeGreaterThan(spiderScore);
  });

  it('maps bathroom-style queries to high-humidity plants', () => {
    const results = rankHybridEntries(entries, 'bathroom plant', 3);
    expect(results[0].entry.name).toBe('Boston Fern');
  });

  it('maps sunny-window queries to sun-loving plants', () => {
    const results = rankHybridEntries(entries, 'plant for sunny window', 3);
    expect(results[0].entry.name).toBe('Aloe Vera');
  });
});
