import { BadRequestException } from '@nestjs/common';
import { AiFlyerParserService } from './ai-flyer-parser.service';

describe('AiFlyerParserService dedupe', () => {
  const service = Object.create(AiFlyerParserService.prototype) as AiFlyerParserService;

  it('dedupes same product with minor offer text differences', () => {
    const items = [
      {
        rawName: 'Kvisttomater',
        normalizedName: 'kvisttomater',
        brand: null,
        category: 'Grönsaker',
        price: 19.9,
        priceUnit: 'kg',
        comparisonPrice: null,
        comparisonUnit: null,
        weight: null,
        bundleWeight: null,
        isBundle: false,
        bundleItems: [],
        offerText: 'Max 2 köp/hushåll',
        confidence: 0.9,
        reasonCodes: ['ai_parsed'],
      },
      {
        rawName: 'KVISTTOMATER',
        normalizedName: 'kvisttomater',
        brand: null,
        category: 'Grönsaker',
        price: 19.9,
        priceUnit: 'kg',
        comparisonPrice: null,
        comparisonUnit: null,
        weight: null,
        bundleWeight: null,
        isBundle: false,
        bundleItems: [],
        offerText: 'Max 2 kop/hushall',
        confidence: 0.89,
        reasonCodes: ['ai_parsed'],
      },
    ];

    const result = (service as any).dedupeItems(items);

    expect(result).toHaveLength(1);
    expect(result[0].normalizedName).toBe('kvisttomater');
  });

  it('keeps products with same name but different prices', () => {
    const items = [
      {
        rawName: 'Kvisttomater',
        normalizedName: 'kvisttomater',
        brand: null,
        category: 'Grönsaker',
        price: 19.9,
        priceUnit: 'kg',
        comparisonPrice: null,
        comparisonUnit: null,
        weight: null,
        bundleWeight: null,
        isBundle: false,
        bundleItems: [],
        offerText: null,
        confidence: 0.9,
        reasonCodes: ['ai_parsed'],
      },
      {
        rawName: 'Kvisttomater',
        normalizedName: 'kvisttomater',
        brand: null,
        category: 'Grönsaker',
        price: 24.9,
        priceUnit: 'kg',
        comparisonPrice: null,
        comparisonUnit: null,
        weight: null,
        bundleWeight: null,
        isBundle: false,
        bundleItems: [],
        offerText: null,
        confidence: 0.9,
        reasonCodes: ['ai_parsed'],
      },
    ];

    const result = (service as any).dedupeItems(items);

    expect(result).toHaveLength(2);
  });

  it('keeps products with same name/price but materially different campaigns', () => {
    const items = [
      {
        rawName: 'Kvisttomater',
        normalizedName: 'kvisttomater',
        brand: null,
        category: 'Grönsaker',
        price: 19.9,
        priceUnit: 'kg',
        comparisonPrice: null,
        comparisonUnit: null,
        weight: null,
        bundleWeight: null,
        isBundle: false,
        bundleItems: [],
        offerText: 'Max 2 köp/hushåll',
        confidence: 0.9,
        reasonCodes: ['ai_parsed'],
      },
      {
        rawName: 'Kvisttomater',
        normalizedName: 'kvisttomater',
        brand: null,
        category: 'Grönsaker',
        price: 19.9,
        priceUnit: 'kg',
        comparisonPrice: null,
        comparisonUnit: null,
        weight: null,
        bundleWeight: null,
        isBundle: false,
        bundleItems: [],
        offerText: 'Ta 3 betala för 2',
        confidence: 0.9,
        reasonCodes: ['ai_parsed'],
      },
    ];

    const result = (service as any).dedupeItems(items);
    expect(result).toHaveLength(2);
  });

  it('keeps bundle and non-bundle as separate entries', () => {
    const items = [
      {
        rawName: 'Fiskpaket',
        normalizedName: 'fiskpaket',
        brand: 'Kapten',
        category: 'Fisk',
        price: 49.9,
        priceUnit: 'pkt',
        comparisonPrice: 83.17,
        comparisonUnit: 'kg',
        weight: null,
        bundleWeight: '600g',
        isBundle: true,
        bundleItems: ['A', 'B'],
        offerText: null,
        confidence: 0.9,
        reasonCodes: ['ai_parsed'],
      },
      {
        rawName: 'Fiskpaket',
        normalizedName: 'fiskpaket',
        brand: 'Kapten',
        category: 'Fisk',
        price: 49.9,
        priceUnit: 'pkt',
        comparisonPrice: 83.17,
        comparisonUnit: 'kg',
        weight: null,
        bundleWeight: null,
        isBundle: false,
        bundleItems: [],
        offerText: null,
        confidence: 0.9,
        reasonCodes: ['ai_parsed'],
      },
    ];

    const result = (service as any).dedupeItems(items);

    expect(result).toHaveLength(2);
  });

  it('throws for empty input in parseWithAI', async () => {
    await expect((service as any).parseWithAI('')).rejects.toBeInstanceOf(
      BadRequestException,
    );
  });
});
