'use strict';
const assert = require('assert');
const Core = require('../core.js');

(function run() {
  let state = Core.resetToEmpty();
  assert.deepEqual(Core.sourcesDue(state).sort(), ['shire3d_com', 'shire3d_com_au', 'shire3d_etsy'].sort());

  const first = Core.applyCatalogSync(state, {
    boundary: 'SHIRE3D_ONLY',
    results: [{
      source: { id: 'shire3d_com', name: 'shire3d.com', url: 'https://shire3d.com/' },
      completedAt: '2026-07-18T03:00:00.000Z',
      products: [{
        name: '3D Printed Dragon Yarn Bowl',
        description: 'A dragon-shaped yarn bowl.',
        price: 35,
        currency: 'AUD',
        imageUrl: 'https://shire3d.com/image.jpg',
        listingUrl: 'https://shire3d.com/product/dragon/ABC123',
        externalId: 'ABC123',
        stockStatus: 'available'
      }]
    }]
  });
  state = first.state;
  assert.equal(first.added, 1);
  assert.equal(state.products.length, 1);
  assert.equal(state.products[0].approved, true);
  assert.equal(state.products[0].trustedSource, true);
  assert.equal(state.products[0].sourceListings.length, 1);
  assert.equal(Core.sourcesDue(state, '2026-07-18T04:00:00.000Z').includes('shire3d_com'), false);

  const second = Core.applyCatalogSync(state, {
    boundary: 'SHIRE3D_ONLY',
    results: [{
      source: { id: 'shire3d_com', name: 'shire3d.com', url: 'https://shire3d.com/' },
      completedAt: '2026-07-18T04:00:00.000Z',
      products: [{
        name: '3D Printed Dragon Yarn Bowl',
        description: 'Updated description.',
        price: 39,
        currency: 'AUD',
        imageUrl: 'https://shire3d.com/image-new.jpg',
        listingUrl: 'https://shire3d.com/product/dragon/ABC123',
        externalId: 'ABC123',
        stockStatus: 'available'
      }]
    }]
  });
  state = second.state;
  assert.equal(second.added, 0);
  assert.equal(second.updated, 1);
  assert.equal(state.products.length, 1);
  assert.equal(state.products[0].price, 39);
  assert.equal(state.products[0].description, 'Updated description.');

  const etsy = Core.applyCatalogSync(state, {
    boundary: 'SHIRE3D_ONLY',
    results: [{
      source: { id: 'shire3d_etsy', name: 'shire3d.etsy.com', url: 'https://www.etsy.com/au/shop/Shire3D' },
      completedAt: '2026-07-18T05:00:00.000Z',
      products: [{
        name: 'Dragon Yarn Bowl',
        description: 'Marketplace listing.',
        price: 35,
        imageUrl: 'https://i.etsystatic.com/image.jpg',
        listingUrl: 'https://www.etsy.com/au/listing/123/dragon-yarn-bowl',
        externalId: '123'
      }]
    }]
  });
  state = etsy.state;
  assert.equal(state.products.length, 1, 'Equivalent website and Etsy names should merge.');
  assert.equal(state.products[0].sourceListings.length, 2);
  assert.equal(state.products[0].sourceId, 'shire3d_com', 'Direct website remains preferred over Etsy.');

  assert.throws(() => Core.applyCatalogSync(state, {
    results: [{ source: { id: 'unknown_source' }, products: [] }]
  }), /allowlist/i);
  assert.throws(() => Core.applyCatalogSync(state, {
    results: [{ source: { id: 'shire3d_com' }, products: [{ name: 'Kitchen Hero recipe export' }] }]
  }), /Kitchen Hero separation boundary/i);

  console.log('PASS: website catalog sync tests');
})();
