import { describe, expect, it } from 'vitest';
import { protectManualNutrition } from '../application/variant-use-cases.js';
import type { ProductRichData } from '../domain/variant.js';

function richData(input: Partial<ProductRichData> = {}): ProductRichData {
  return {
    productId: 'product-id',
    ingredients: null,
    allergens: [],
    nutrition: null,
    nutritionSource: null,
    isOrganic: false,
    organicCertification: null,
    createdAt: new Date('2026-01-01T00:00:00Z'),
    updatedAt: new Date('2026-01-01T00:00:00Z'),
    ...input,
  };
}

describe('rich data use cases', () => {
  it('keeps manual nutrition when an external source tries to overwrite it', () => {
    const current = richData({
      nutrition: { calories: 100 },
      nutritionSource: 'manual',
    });

    const patch = protectManualNutrition(current, {
      nutrition: { calories: 200 },
      nutritionSource: 'openfoodfacts',
      ingredients: 'Updated ingredients',
    });

    expect(patch).toEqual({ ingredients: 'Updated ingredients' });
  });

  it('allows manual nutrition to overwrite external nutrition', () => {
    const current = richData({
      nutrition: { calories: 100 },
      nutritionSource: 'openfoodfacts',
    });

    const patch = protectManualNutrition(current, {
      nutrition: { calories: 200 },
      nutritionSource: 'manual',
    });

    expect(patch).toEqual({ nutrition: { calories: 200 }, nutritionSource: 'manual' });
  });
});
