import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SCRIPTS_DIR = path.join(__dirname, '..', 'scripts');
const LOG_FILE = path.join(__dirname, 'check-tests-log.txt');

const log = [];

function writeLog(message) {
  console.log(message);
  log.push(message);
}

function getAllJsFiles(dir) {
  const files = [];

  function traverse(currentDir) {
    const entries = fs.readdirSync(currentDir, { withFileTypes: true });

    for (const entry of entries) {
      const fullPath = path.join(currentDir, entry.name);

      if (entry.isDirectory()) {
        traverse(fullPath);
      } else if (entry.isFile() && entry.name.endsWith('.js')) {
        files.push(fullPath);
      }
    }
  }

  traverse(dir);
  return files;
}

function checkForTests(filePath) {
  const content = fs.readFileSync(filePath, 'utf-8');
  return content.includes('let Tests');
}

// Programme principal
writeLog('='.repeat(60));
writeLog('VÉRIFICATION DE "let Tests" DANS LES SCRIPTS');
writeLog('='.repeat(60));
writeLog(`Date: ${new Date().toISOString()}`);
writeLog(`Répertoire: ${SCRIPTS_DIR}\n`);

const allFiles = getAllJsFiles(SCRIPTS_DIR);
writeLog(`Total de fichiers trouvés: ${allFiles.length}\n`);

const filesWithTests = [];
const filesWithoutTests = [];

allFiles.forEach(filePath => {
  const relativePath = path.relative(path.join(__dirname, '..'), filePath);

  if (checkForTests(filePath)) {
    filesWithTests.push(relativePath);
  } else {
    filesWithoutTests.push(relativePath);
  }
});

// Afficher les résultats
writeLog('=== FICHIERS CONTENANT "let Tests" ===\n');
if (filesWithTests.length > 0) {
  filesWithTests.forEach(file => {
    writeLog(`✓ ${file}`);
  });
} else {
  writeLog('Aucun fichier trouvé.');
}

writeLog(`\nTotal: ${filesWithTests.length} fichier(s)\n`);

writeLog('='.repeat(60));
writeLog('=== FICHIERS NE CONTENANT PAS "let Tests" ===\n');
if (filesWithoutTests.length > 0) {
  filesWithoutTests.forEach(file => {
    writeLog(`✗ ${file}`);
  });
} else {
  writeLog('Aucun fichier trouvé.');
}

writeLog(`\nTotal: ${filesWithoutTests.length} fichier(s)\n`);

writeLog('='.repeat(60));
writeLog('RÉSUMÉ');
writeLog('='.repeat(60));
writeLog(`Fichiers avec "let Tests": ${filesWithTests.length}`);
writeLog(`Fichiers sans "let Tests": ${filesWithoutTests.length}`);
writeLog(`Total de fichiers analysés: ${allFiles.length}`);
writeLog('='.repeat(60));

// Sauvegarder le log
fs.writeFileSync(LOG_FILE, log.join('\n'), 'utf-8');
writeLog(`\nLog sauvegardé dans: ${LOG_FILE}`);

// Exporter les résultats en JSON
const results = {
  date: new Date().toISOString(),
  totalFiles: allFiles.length,
  filesWithTests: filesWithTests,
  filesWithoutTests: filesWithoutTests
};

const resultsFile = path.join(__dirname, 'check-tests-results.json');
fs.writeFileSync(resultsFile, JSON.stringify(results, null, 2), 'utf-8');
writeLog(`Résultats sauvegardés dans: ${resultsFile}`);
