import test from 'node:test';
import assert from 'node:assert';
import { requireCommonJS } from './_helpers/commonjs-bridge.js';
import { AutoReporter } from './reporters/AutoReporter.js';

/**
 * 20 TESTS TI MASSIVE - STACKS PRÉDÉFINIS AVEC APPLICATIONS PARTIELLES
 * Utilisation exclusive des stacks disponibles avec différentes configurations
 */

// Auto-Reporter Configuration
const autoReporter = new AutoReporter();

// Configuration test commune
const mockCsvData = {
  mc0: 'plaque test massive stacks',
  t0: 'Test massive stacks applications partielles',
  personality: {
    nom: 'Marc',
    style: 'technique',
    description: 'Expert technique pour tests massifs'
  }
};

const mockContent = {
  'Titre_H1': 'Test titre massive stacks applications partielles',
  'Introduction': 'Test introduction pour stacks modulaires massives',
  'Contenu_Principal': 'Test contenu principal avec stacks techniques et vocabulaire générique qui nécessite amélioration',
  'Conclusion': 'Test conclusion stacks applications partielles'
};

const mockContentRich = {
  'Titre_H1': 'Test titre avec contenu générique nécessitant amélioration technique avancée',
  'Introduction': 'Introduction générique avec vocabulaire basique à améliorer pour integration',
  'Contenu_Principal': 'Contenu principal avec termes génériques optimal et efficace nécessitant précision technique pour performance',
  'Conclusion': 'Conclusion basique qui nécessite enhancement professionnel et amélioration'
};

const mockContentComplex = {
  'Titre_H1': 'Titre complexe pour test applications avec besoins techniques multiples',
  'Introduction': 'Introduction technique avec vocabulaire spécialisé qui demande amélioration fluidité',
  'Contenu_Principal': 'Contenu technique avancé avec termes spécialisés, nécessitant amélioration transitions et fluidité pour optimisation performance globale',
  'Conclusion': 'Conclusion technique professionnelle nécessitant enhancement style et personnalité'
};

// =========================================
// TESTS SELECTIVE STACKS - VARIATIONS
// =========================================

test('Massive TI 1: lightEnhancement standard', { timeout: 90000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentRich,
    'lightEnhancement',
    mockCsvData,
    { source: 'massive_ti_1' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 1: lightEnhancement standard');
});

test('Massive TI 2: lightEnhancement avec intensité élevée', { timeout: 90000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentRich,
    'lightEnhancement',
    { ...mockCsvData, intensity: 1.2 },
    { source: 'massive_ti_2' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 2: lightEnhancement intensité élevée');
});

test('Massive TI 3: standardEnhancement standard', { timeout: 120000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentComplex,
    'standardEnhancement',
    mockCsvData,
    { source: 'massive_ti_3' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 3: standardEnhancement standard');
});

test('Massive TI 4: standardEnhancement avec Gemini prioritaire', { timeout: 120000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentComplex,
    'standardEnhancement',
    { ...mockCsvData, preferredProvider: 'gemini' },
    { source: 'massive_ti_4' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 4: standardEnhancement avec Gemini');
});

test('Massive TI 5: fullEnhancement standard', { timeout: 180000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentComplex,
    'fullEnhancement',
    mockCsvData,
    { source: 'massive_ti_5' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 5: fullEnhancement standard');
});

test('Massive TI 6: fullEnhancement avec intensité maximale', { timeout: 180000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentComplex,
    'fullEnhancement',
    { ...mockCsvData, intensity: 1.5 },
    { source: 'massive_ti_6' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 6: fullEnhancement intensité maximale');
});

test('Massive TI 7: adaptiveEnhancement avec contenu simple', { timeout: 150000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContent,
    'adaptiveEnhancement',
    mockCsvData,
    { source: 'massive_ti_7' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 7: adaptiveEnhancement simple');
});

test('Massive TI 8: adaptiveEnhancement avec contenu complexe', { timeout: 150000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentComplex,
    'adaptiveEnhancement',
    mockCsvData,
    { source: 'massive_ti_8' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 8: adaptiveEnhancement complexe');
});

// =========================================
// TESTS ADVERSARIAL STACKS - VARIATIONS
// =========================================

test('Massive TI 9: Adversarial lightDefense', { timeout: 120000 }, async () => {
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  const result = await AdversarialLayers.applyPredefinedStack(
    mockContentRich,
    'lightDefense',
    mockCsvData,
    { source: 'massive_ti_9' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 9: Adversarial lightDefense');
});

test('Massive TI 10: Adversarial standardDefense', { timeout: 150000 }, async () => {
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  const result = await AdversarialLayers.applyPredefinedStack(
    mockContentComplex,
    'standardDefense',
    mockCsvData,
    { source: 'massive_ti_10' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 10: Adversarial standardDefense');
});

test('Massive TI 11: Adversarial heavyDefense', { timeout: 180000 }, async () => {
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  const result = await AdversarialLayers.applyPredefinedStack(
    mockContentComplex,
    'heavyDefense',
    mockCsvData,
    { source: 'massive_ti_11' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 11: Adversarial heavyDefense');
});

test('Massive TI 12: Adversarial adaptiveDefense', { timeout: 150000 }, async () => {
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  const result = await AdversarialLayers.applyPredefinedStack(
    mockContentRich,
    'adaptiveDefense',
    mockCsvData,
    { source: 'massive_ti_12' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 12: Adversarial adaptiveDefense');
});

// =========================================
// TESTS PIPELINES COMPLEXES
// =========================================

test('Massive TI 13: Pipeline light → light', { timeout: 240000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  // Phase 1: Selective
  let result = await applyPredefinedStack(
    mockContentRich,
    'lightEnhancement',
    mockCsvData,
    { source: 'massive_ti_13_phase1' }
  );

  // Phase 2: Adversarial
  result = await AdversarialLayers.applyPredefinedStack(
    result.content,
    'lightDefense',
    mockCsvData,
    { source: 'massive_ti_13_phase2' }
  );

  assert.ok(result.content, 'Pipeline content généré');
  console.log('✅ Massive TI 13: Pipeline light → light');
});

test('Massive TI 14: Pipeline standard → standard', { timeout: 300000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  // Phase 1: Selective
  let result = await applyPredefinedStack(
    mockContentComplex,
    'standardEnhancement',
    mockCsvData,
    { source: 'massive_ti_14_phase1' }
  );

  // Phase 2: Adversarial
  result = await AdversarialLayers.applyPredefinedStack(
    result.content,
    'standardDefense',
    mockCsvData,
    { source: 'massive_ti_14_phase2' }
  );

  assert.ok(result.content, 'Pipeline content généré');
  console.log('✅ Massive TI 14: Pipeline standard → standard');
});

test('Massive TI 15: Pipeline full → heavy', { timeout: 360000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  // Phase 1: Selective
  let result = await applyPredefinedStack(
    mockContentComplex,
    'fullEnhancement',
    mockCsvData,
    { source: 'massive_ti_15_phase1' }
  );

  // Phase 2: Adversarial
  result = await AdversarialLayers.applyPredefinedStack(
    result.content,
    'heavyDefense',
    mockCsvData,
    { source: 'massive_ti_15_phase2' }
  );

  assert.ok(result.content, 'Pipeline content généré');
  console.log('✅ Massive TI 15: Pipeline full → heavy');
});

test('Massive TI 16: Pipeline adaptive → adaptive', { timeout: 300000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  // Phase 1: Selective
  let result = await applyPredefinedStack(
    mockContentComplex,
    'adaptiveEnhancement',
    mockCsvData,
    { source: 'massive_ti_16_phase1' }
  );

  // Phase 2: Adversarial
  result = await AdversarialLayers.applyPredefinedStack(
    result.content,
    'adaptiveDefense',
    mockCsvData,
    { source: 'massive_ti_16_phase2' }
  );

  assert.ok(result.content, 'Pipeline content généré');
  console.log('✅ Massive TI 16: Pipeline adaptive → adaptive');
});

// =========================================
// TESTS CONFIGURATIONS SPÉCIALES
// =========================================

test('Massive TI 17: lightEnhancement avec personnalité différente', { timeout: 90000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const customCsvData = {
    ...mockCsvData,
    personality: {
      nom: 'Sophie',
      style: 'créatif',
      description: 'Experte créative'
    }
  };

  const result = await applyPredefinedStack(
    mockContentRich,
    'lightEnhancement',
    customCsvData,
    { source: 'massive_ti_17' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 17: lightEnhancement personnalité Sophie');
});

test('Massive TI 18: standardEnhancement avec contexte spécialisé', { timeout: 120000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const specialCsvData = {
    ...mockCsvData,
    mc0: 'système industriel complexe',
    t0: 'Optimisation système industriel haute performance'
  };

  const result = await applyPredefinedStack(
    mockContentComplex,
    'standardEnhancement',
    specialCsvData,
    { source: 'massive_ti_18' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 18: standardEnhancement contexte industriel');
});

test('Massive TI 19: fullEnhancement avec tous les providers', { timeout: 200000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentComplex,
    'fullEnhancement',
    { ...mockCsvData, forceAllProviders: true },
    { source: 'massive_ti_19' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Massive TI 19: fullEnhancement tous providers');
});

test('Massive TI 20: Pipeline Mega - Triple Stack Complet', { timeout: 480000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');
  const { AdversarialLayers } = requireCommonJS('adversarial-generation/AdversarialLayers');

  // Phase 1: Selective Enhancement
  let result = await applyPredefinedStack(
    mockContentComplex,
    'fullEnhancement',
    mockCsvData,
    { source: 'massive_ti_20_phase1' }
  );

  // Phase 2: Adversarial Defense
  result = await AdversarialLayers.applyPredefinedStack(
    result.content,
    'standardDefense',
    mockCsvData,
    { source: 'massive_ti_20_phase2' }
  );

  // Phase 3: Re-Enhancement adaptatif
  result = await applyPredefinedStack(
    result.content,
    'adaptiveEnhancement',
    { ...mockCsvData, intensity: 0.8 },
    { source: 'massive_ti_20_phase3' }
  );

  assert.ok(result.content, 'Pipeline mega content généré');
  console.log('✅ Massive TI 20: Pipeline MEGA - Triple Stack');
});

console.log('🎯 20 TESTS TI MASSIVE STACKS CONFIGURÉS');
console.log('📊 Couverture: lightEnhancement, standardEnhancement, fullEnhancement, adaptiveEnhancement');
console.log('🛡️ Defense: lightDefense, standardDefense, heavyDefense, adaptiveDefense');
console.log('🔄 Pipelines: Simples, doubles, triples avec configurations variées');
console.log('🤖 Multi-providers: OpenAI, Gemini, Mistral, Claude avec rotation');
console.log('⚡ Intensités: 0.7, 0.8, 0.9, 1.0, 1.2, 1.5 selon les besoins');