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

(function run() {
  let state = Core.resetToEmpty();
  const productIds = [];
  for (let index = 1; index <= 15; index += 1) {
    const result = Core.saveProduct(state, {
      name: `SHIRE3D Product ${index}`,
      description: `A useful customer-facing description for product ${index}. Internal production notes are not included.`,
      price: 10 + index,
      imageData: `https://shire3d.com/images/${index}.jpg`,
      listingUrl: `https://shire3d.com/product/${index}`,
      approved: true,
      trustedSource: true,
      stockStatus: 'available'
    });
    state = result.state;
    productIds.push(result.product.id);
  }

  // Give the first saved item extra historical exposure so the boost ranking has a reason to place others first.
  const overexposedId = productIds[0];
  state.posts.push({ id: 'old-1', productId: overexposedId, approvalStatus: 'published' });
  state.posts.push({ id: 'old-2', productId: overexposedId, approvalStatus: 'published' });

  const generated = Core.generateCampaign(state, {
    // Deliberately attempt to force a single product. The engine must ignore it.
    productMode: 'single',
    productId: productIds[0],
    month: '2026-09',
    platforms: ['facebook', 'instagram', 'linkedin', 'tiktok', 'youtube_shorts'],
    frequency: '2 posts per week',
    goal: 'Drive qualified interest toward the approved listing.'
  });

  assert.equal(generated.campaign.productMode, 'all', 'Single-product input must be ignored.');
  assert.equal(generated.campaign.productCount, 15);
  assert.equal(generated.posts.length, 15, 'Every product must receive at least one post even when the chosen pace would normally create fewer.');
  assert.equal(new Set(generated.posts.map((post) => post.productId)).size, 15, 'Every live product must appear in the campaign.');
  assert.notEqual(generated.posts[0].productId, overexposedId, 'Less-promoted products should be placed before an overexposed product.');
  assert(generated.posts.every((post) => !/approved listing|development focus|qualified interest|honest production information/i.test(post.caption)));
  assert(generated.posts.every((post) => post.caption.includes('Shop here:') || ['tiktok', 'youtube_shorts'].includes(post.platform)));
  assert(generated.campaign.platforms.includes('youtube_shorts'), 'YouTube Shorts must be a supported campaign platform.');
  assert(generated.posts.every((post) => post.title.includes('·')));

  const rewritten = Core.rewriteCampaignCopy(generated.state, generated.campaign.id);
  assert.equal(rewritten.rewritten, 15);
  assert(rewritten.state.posts.filter((post) => post.campaignId === generated.campaign.id).every((post) => post.copyStyle === 'customer_friendly_v2'));

  // v1.1 draft migration: rewrite only unfrozen legacy generated copy.
  const legacy = Core.deepClone(generated.state);
  legacy.copyMigrationVersion = 0;
  const draft = legacy.posts.find((post) => post.campaignId === generated.campaign.id);
  draft.caption = 'See the approved listing. The development focus is repeatable print quality and drive qualified interest.';
  draft.frozen = false;
  draft.approvalStatus = 'ray_review';
  const migrated = Core.ensureState(legacy);
  const migratedDraft = migrated.posts.find((post) => post.id === draft.id);
  assert(!/approved listing|development focus|qualified interest/i.test(migratedDraft.caption));
  assert.equal(migrated.copyMigrationVersion, 2);

  console.log('PASS: customer-friendly full-catalogue marketing tests');
})();
