const ComparisonGenerator = require('../generators/comparison');
const fs = require('fs').promises;
const path = require('path');

const generator = new ComparisonGenerator();

const sampleComparisons = [
  ['Semrush', 'Ahrefs'],
  ['Monday', 'Asana'],
  ['Shopify', 'WooCommerce'],
  ['Notion', 'Obsidian'],
  ['Figma', 'Canva'],
  ['HubSpot', 'Salesforce'],
  ['Slack', 'Microsoft Teams'],
  ['Trello', 'Jira'],
  ['Mailchimp', 'SendGrid'],
  ['Stripe', 'PayPal']
];

async function generateSamples() {
  console.log('');
  console.log('╔════════════════════════════════════════╗');
  console.log('║     CompareKit Sample Generator        ║');
  console.log('╚════════════════════════════════════════╝');
  console.log('');
  console.log(`Generating ${sampleComparisons.length} sample images...\n`);

  const outputDir = path.join(__dirname, '../../generated/samples');
  await fs.mkdir(outputDir, { recursive: true });

  let successCount = 0;
  let failCount = 0;

  for (let i = 0; i < sampleComparisons.length; i++) {
    const [productA, productB] = sampleComparisons[i];

    try {
      console.log(`[${i + 1}/${sampleComparisons.length}] Generating: ${productA} vs ${productB}`);

      const buffer = await generator.generate(productA, productB);

      const filename = `${productA.toLowerCase()}-vs-${productB.toLowerCase()}.png`;
      const filepath = path.join(outputDir, filename);

      await fs.writeFile(filepath, buffer);

      console.log(`  ✓ Saved: ${filename}`);
      successCount++;

    } catch (error) {
      console.error(`  ✗ Failed: ${productA} vs ${productB}`, error.message);
      failCount++;
    }

    console.log('');
  }

  console.log('═══════════════════════════════════════');
  console.log(`✅ Success: ${successCount}`);
  console.log(`❌ Failed: ${failCount}`);
  console.log(`📂 Output: ${outputDir}`);
  console.log('');

  // Show variety stats
  const history = generator.randomizer.getHistory();
  console.log('📊 Variety Analysis:');
  console.log(`  • Total generations: ${history.length}`);

  const backgrounds = new Set(history.map(h => h.background?.id));
  const colorSchemes = new Set(history.map(h => h.colorScheme?.id));
  const layouts = new Set(history.map(h => h.layout?.id));

  console.log(`  • Unique backgrounds: ${backgrounds.size}`);
  console.log(`  • Unique color schemes: ${colorSchemes.size}`);
  console.log(`  • Unique layouts: ${layouts.size}`);
  console.log('');
}

generateSamples().catch(error => {
  console.error('Sample generation failed:', error);
  process.exit(1);
});
