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

/**
 * 10 TESTS TI RAPIDES - ÉVITE GEMINI POUR PERFORMANCE
 * Focus OpenAI + Claude uniquement pour rapports rapides
 */

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

// Configuration test rapide
const mockCsvData = {
  mc0: 'plaque test rapide',
  t0: 'Test rapide applications partielles',
  personality: {
    nom: 'Marc',
    style: 'technique',
    description: 'Expert technique rapide'
  }
};

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 rapidement',
  'Contenu_Principal': 'Contenu principal avec termes génériques optimal et efficace nécessitant précision technique urgente',
  'Conclusion': 'Conclusion basique qui nécessite enhancement professionnel rapide'
};

console.log('🚀 10 TESTS TI RAPIDES - FOCUS PERFORMANCE');
console.log('⚡ OpenAI + Claude seulement (évite Gemini timeout)');

// =========================================
// TESTS SELECTIVE RAPIDES - OPENAI SEULEMENT
// =========================================

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

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

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

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

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

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

test('Rapide TI 3: lightEnhancement Claude', { timeout: 60000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const result = await applyPredefinedStack(
    mockContentRich,
    'lightEnhancement',
    { ...mockCsvData, preferredProvider: 'claude' },
    { source: 'rapide_ti_3' }
  );

  assert.ok(result.content, 'Content généré');
  console.log('✅ Rapide TI 3: lightEnhancement Claude');
});

// =========================================
// TESTS ADVERSARIAL RAPIDES
// =========================================

test('Rapide TI 4: Adversarial lightDefense', { timeout: 60000 }, async () => {
  const AdversarialLayers = requireCommonJS('adversarial-generation/AdversarialLayers');

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

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

test('Rapide TI 5: Adversarial standardDefense', { timeout: 90000 }, async () => {
  const AdversarialLayers = requireCommonJS('adversarial-generation/AdversarialLayers');

  const result = await AdversarialLayers.applyPredefinedStack(
    mockContentRich,
    'standardDefense',
    { ...mockCsvData, preferredProvider: 'claude' },
    { source: 'rapide_ti_5' }
  );

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

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

test('Rapide TI 6: Personnalité Sophie OpenAI', { timeout: 45000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

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

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

  assert.ok(result.content, 'Content généré');
  console.log('✅ Rapide TI 6: Personnalité Sophie OpenAI');
});

test('Rapide TI 7: Contexte industriel', { timeout: 45000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  const specialCsvData = {
    ...mockCsvData,
    mc0: 'système industriel rapide',
    t0: 'Optimisation système industriel performance',
    preferredProvider: 'openai'
  };

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

  assert.ok(result.content, 'Content généré');
  console.log('✅ Rapide TI 7: Contexte industriel');
});

// =========================================
// TESTS PIPELINES RAPIDES
// =========================================

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

  // Phase 1: Selective (OpenAI)
  let result = await applyPredefinedStack(
    mockContentRich,
    'lightEnhancement',
    { ...mockCsvData, preferredProvider: 'openai' },
    { source: 'rapide_ti_8_phase1' }
  );

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

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

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

  // Phase 1: OpenAI
  let result = await applyPredefinedStack(
    mockContentRich,
    'lightEnhancement',
    { ...mockCsvData, preferredProvider: 'openai', intensity: 0.8 },
    { source: 'rapide_ti_9_phase1' }
  );

  // Phase 2: Claude
  result = await applyPredefinedStack(
    result.content,
    'lightEnhancement',
    { ...mockCsvData, preferredProvider: 'claude', intensity: 0.9 },
    { source: 'rapide_ti_9_phase2' }
  );

  assert.ok(result.content, 'Double enhancement content généré');
  console.log('✅ Rapide TI 9: Double lightEnhancement');
});

test('Rapide TI 10: Test stress multiple providers', { timeout: 90000 }, async () => {
  const { applyPredefinedStack } = requireCommonJS('selective-enhancement/SelectiveLayers');

  // Test avec rotation de providers
  const providers = ['openai', 'claude', 'openai'];
  let result = { content: mockContentRich };

  for (let i = 0; i < providers.length; i++) {
    result = await applyPredefinedStack(
      result.content,
      'lightEnhancement',
      { ...mockCsvData, preferredProvider: providers[i], intensity: 0.7 + (i * 0.1) },
      { source: `rapide_ti_10_phase${i + 1}` }
    );
  }

  assert.ok(result.content, 'Multi-provider content généré');
  console.log('✅ Rapide TI 10: Test stress multiple providers');
});

console.log('⚡ Tests optimisés pour performance - évite les timeouts Gemini');
console.log('🎯 Timeouts réduits : 45-120 secondes maximum par test');
console.log('🚀 Focus OpenAI (rapide) + Claude (qualité) seulement');