import { parseExcelFile, importToDatabase } from './server/importExcel';
import { readFileSync } from 'fs';

async function testImport() {
  console.log('=== TEST IMPORT DIRECT ===\n');
  
  const buffer = readFileSync('/home/ubuntu/upload/template_import_formations_test.xlsx');
  const { formations, sequences } = parseExcelFile(buffer);
  
  console.log(`Formations parsées: ${formations.length}`);
  formations.forEach((f, i) => {
    console.log(`  ${i}: "${f.nom}" - "${f.description}" - public: "${f.publicCible}"`);
  });
  
  console.log(`\nSéquences parsées: ${sequences.length}`);
  sequences.forEach((s, i) => {
    console.log(`  ${i}: formation="${s.formationNom}", nom="${s.nom}"`);
  });
  
  console.log('\n=== DÉBUT IMPORT EN BASE ===\n');
  const result = await importToDatabase(formations, sequences);
  
  console.log('\n=== RÉSULTAT ===');
  console.log(`Success: ${result.success}`);
  console.log(`Formations créées: ${result.formationsCreated}`);
  console.log(`Séquences créées: ${result.sequencesCreated}`);
  
  if (result.errors.length > 0) {
    console.log('\nErreurs:');
    result.errors.forEach(e => console.log(`  - ${e}`));
  }
  
  if (result.warnings.length > 0) {
    console.log('\nAvertissements:');
    result.warnings.forEach(w => console.log(`  - ${w}`));
  }
}

testImport().then(() => {
  console.log('\n=== TEST TERMINÉ ===');
  process.exit(0);
}).catch(err => {
  console.error('\n=== ERREUR FATALE ===');
  console.error(err);
  process.exit(1);
});
