// ========================================
// TEST PIPELINE COMPLET AVEC TENDANCES
// Test du pipeline complet : Google Sheets + Digital Ocean + Tendances + PromptEngine
// ========================================

const { handleFullWorkflow } = require('../lib/Main');
const { TrendManager } = require('../lib/trend-prompts/TrendManager');
const { DynamicPromptEngine } = require('../lib/prompt-engine/DynamicPromptEngine');

async function testFullPipelineWithTrends() {
  console.log('🚀 === TEST PIPELINE COMPLET AVEC TENDANCES ===\n');

  // ========================================
  // TEST 1: PIPELINE STANDARD (BASELINE)
  // ========================================
  console.log('📊 === TEST 1: PIPELINE STANDARD (BASELINE) ===');

  try {
    console.log('⏳ Exécution pipeline standard ligne 2...');

    const baselineResult = await handleFullWorkflow({
      rowNumber: 2,
      source: 'test_full_pipeline_baseline',
      selectiveStack: 'standardEnhancement',
      adversarialMode: 'light',
      humanSimulationMode: 'none',
      patternBreakingMode: 'none',
      intensity: 1.0,
      saveIntermediateSteps: true
    });

    console.log('✅ Pipeline standard terminé:');
    console.log(`  • Success: ${baselineResult.success}`);
    console.log(`  • Article ID: ${baselineResult.articleId}`);
    console.log(`  • Version finale: ${baselineResult.finalVersion}`);
    console.log(`  • Mots: ${baselineResult.wordCount}`);
    console.log(`  • Durée: ${baselineResult.duration}ms`);

    if (baselineResult.finalText) {
      console.log(`  • Aperçu: ${baselineResult.finalText.substring(0, 150)}...`);
    }

  } catch (error) {
    console.log(`❌ Erreur pipeline standard: ${error.message}`);
  }

  console.log('\n' + '='.repeat(80) + '\n');

  // ========================================
  // TEST 2: PIPELINE AVEC TENDANCE ECO-RESPONSABLE
  // ========================================
  console.log('🌱 === TEST 2: PIPELINE AVEC TENDANCE ECO-RESPONSABLE ===');

  try {
    console.log('⏳ Exécution pipeline avec tendance eco-responsable...');

    const ecoResult = await handleFullWorkflow({
      rowNumber: 3,
      source: 'test_full_pipeline_eco',
      selectiveStack: 'standardEnhancement',
      adversarialMode: 'light',
      humanSimulationMode: 'lightSimulation',
      patternBreakingMode: 'syntaxFocus',
      intensity: 1.1,
      trendId: 'eco-responsable',  // ← NOUVELLE TENDANCE
      saveIntermediateSteps: true
    });

    console.log('✅ Pipeline eco-responsable terminé:');
    console.log(`  • Success: ${ecoResult.success}`);
    console.log(`  • Article ID: ${ecoResult.articleId}`);
    console.log(`  • Version finale: ${ecoResult.finalVersion}`);
    console.log(`  • Mots: ${ecoResult.wordCount}`);
    console.log(`  • Durée: ${ecoResult.duration}ms`);

    if (ecoResult.finalText) {
      console.log(`  • Aperçu: ${ecoResult.finalText.substring(0, 150)}...`);
    }

  } catch (error) {
    console.log(`❌ Erreur pipeline eco: ${error.message}`);
  }

  console.log('\n' + '='.repeat(80) + '\n');

  // ========================================
  // TEST 3: PIPELINE AVEC TENDANCE TECH-INNOVATION
  // ========================================
  console.log('🚀 === TEST 3: PIPELINE AVEC TENDANCE TECH-INNOVATION ===');

  try {
    console.log('⏳ Exécution pipeline avec tendance tech-innovation...');

    const techResult = await handleFullWorkflow({
      rowNumber: 4,
      source: 'test_full_pipeline_tech',
      selectiveStack: 'fullEnhancement',
      adversarialMode: 'standard',
      humanSimulationMode: 'personalityFocus',
      patternBreakingMode: 'connectorsFocus',
      intensity: 1.2,
      trendId: 'tech-innovation',  // ← TENDANCE TECH
      saveIntermediateSteps: true
    });

    console.log('✅ Pipeline tech-innovation terminé:');
    console.log(`  • Success: ${techResult.success}`);
    console.log(`  • Article ID: ${techResult.articleId}`);
    console.log(`  • Version finale: ${techResult.finalVersion}`);
    console.log(`  • Mots: ${techResult.wordCount}`);
    console.log(`  • Durée: ${techResult.duration}ms`);

    if (techResult.finalText) {
      console.log(`  • Aperçu: ${techResult.finalText.substring(0, 150)}...`);
    }

  } catch (error) {
    console.log(`❌ Erreur pipeline tech: ${error.message}`);
  }

  console.log('\n' + '='.repeat(80) + '\n');

  // ========================================
  // TEST 4: PIPELINE ARTISANAL-PREMIUM INTENSIF
  // ========================================
  console.log('🎨 === TEST 4: PIPELINE ARTISANAL-PREMIUM INTENSIF ===');

  try {
    console.log('⏳ Exécution pipeline artisanal premium intensif...');

    const artisanalResult = await handleFullWorkflow({
      rowNumber: 5,
      source: 'test_full_pipeline_artisanal',
      selectiveStack: 'personalityFocus',
      adversarialMode: 'heavy',
      humanSimulationMode: 'adaptive',
      patternBreakingMode: 'adaptive',
      intensity: 1.4,
      trendId: 'artisanal-premium',  // ← TENDANCE ARTISANALE
      saveIntermediateSteps: true
    });

    console.log('✅ Pipeline artisanal-premium terminé:');
    console.log(`  • Success: ${artisanalResult.success}`);
    console.log(`  • Article ID: ${artisanalResult.articleId}`);
    console.log(`  • Version finale: ${artisanalResult.finalVersion}`);
    console.log(`  • Mots: ${artisanalResult.wordCount}`);
    console.log(`  • Durée: ${artisanalResult.duration}ms`);

    if (artisanalResult.finalText) {
      console.log(`  • Aperçu: ${artisanalResult.finalText.substring(0, 150)}...`);
    }

  } catch (error) {
    console.log(`❌ Erreur pipeline artisanal: ${error.message}`);
  }

  console.log('\n' + '='.repeat(80) + '\n');

  // ========================================
  // TEST 5: COMPARAISON DES RÉSULTATS
  // ========================================
  console.log('⚖️ === COMPARAISON DES RÉSULTATS ===');

  console.log('📊 Résumé comparatif:');
  console.log('┌─────────────────┬──────────┬────────────┬──────────┬─────────────┐');
  console.log('│ Configuration   │ Article  │ Mots       │ Durée    │ Tendance    │');
  console.log('├─────────────────┼──────────┼────────────┼──────────┼─────────────┤');
  console.log('│ Standard        │ Ligne 2  │ Variable   │ Variable │ Aucune      │');
  console.log('│ Eco-responsable │ Ligne 3  │ Variable   │ Variable │ Écologie    │');
  console.log('│ Tech-innovation │ Ligne 4  │ Variable   │ Variable │ Technologie │');
  console.log('│ Artisanal       │ Ligne 5  │ Variable   │ Variable │ Premium     │');
  console.log('└─────────────────┴──────────┴────────────┴──────────┴─────────────┘');

  // ========================================
  // TEST 6: VALIDATION TENDANCES APPLIQUÉES
  // ========================================
  console.log('\n🔍 === VALIDATION DES TENDANCES ===');

  const trendManager = new TrendManager();

  console.log('📋 Tendances utilisées dans les tests:');

  // Test eco-responsable
  await trendManager.setTrend('eco-responsable');
  const ecoTrend = trendManager.getCurrentTrend();
  console.log(`\n🌱 ${ecoTrend.name}:`);
  console.log(`  • Termes cibles: ${ecoTrend.config.technical.targetTerms.join(', ')}`);
  console.log(`  • Style: ${ecoTrend.config.style.targetStyle}`);

  // Test tech-innovation
  await trendManager.setTrend('tech-innovation');
  const techTrend = trendManager.getCurrentTrend();
  console.log(`\n🚀 ${techTrend.name}:`);
  console.log(`  • Termes cibles: ${techTrend.config.technical.targetTerms.join(', ')}`);
  console.log(`  • Style: ${techTrend.config.style.targetStyle}`);

  // Test artisanal-premium
  await trendManager.setTrend('artisanal-premium');
  const artisanalTrend = trendManager.getCurrentTrend();
  console.log(`\n🎨 ${artisanalTrend.name}:`);
  console.log(`  • Termes cibles: ${artisanalTrend.config.technical.targetTerms.join(', ')}`);
  console.log(`  • Style: ${artisanalTrend.config.style.targetStyle}`);

  console.log('\n🎯 === TEST PIPELINE COMPLET TERMINÉ ===');
  console.log('✨ Fonctionnalités testées:');
  console.log('  • ✅ Google Sheets data loading');
  console.log('  • ✅ Digital Ocean XML templates');
  console.log('  • ✅ Système de tendances modulaires');
  console.log('  • ✅ Pipeline modulaire complet');
  console.log('  • ✅ Sauvegarde versionnée (v1.0→v2.0)');
  console.log('  • ✅ Comparaison multi-configurations');
}

// FONCTION HELPER - Test pipeline simple
async function quickPipelineTest(rowNumber, trendId = null) {
  console.log(`\n🚀 Test rapide pipeline ligne ${rowNumber}${trendId ? ` (${trendId})` : ''}`);

  try {
    const result = await handleFullWorkflow({
      rowNumber,
      source: `quick_test_${rowNumber}`,
      selectiveStack: 'standardEnhancement',
      adversarialMode: 'light',
      intensity: 1.0,
      trendId,
      saveIntermediateSteps: false
    });

    console.log('✅ Résultat:');
    console.log(`  • Article ID: ${result.articleId}`);
    console.log(`  • Mots: ${result.wordCount}`);
    console.log(`  • Durée: ${result.duration}ms`);

    return result;

  } catch (error) {
    console.log(`❌ Erreur: ${error.message}`);
    return null;
  }
}

// EXÉCUTER TEST
if (require.main === module) {
  testFullPipelineWithTrends()
    .catch(console.error);
}

// Export pour usage externe
module.exports = { testFullPipelineWithTrends, quickPipelineTest };