const fs = require('fs');
const path = require('path');

function analyzeRoutes(dir, basePath = '') {
  const routes = [];
  const items = fs.readdirSync(dir);
  
  for (const item of items) {
    const fullPath = path.join(dir, item);
    const stat = fs.statSync(fullPath);
    
    if (stat.isDirectory()) {
      // Route groups (main) ne font pas partie de l'URL
      if (item.startsWith('(') && item.endsWith(')')) {
        routes.push(...analyzeRoutes(fullPath, basePath));
      } else {
        routes.push(...analyzeRoutes(fullPath, path.join(basePath, item)));
      }
    } else if (item === 'page.tsx' || item === 'page.js') {
      const route = basePath || '/';
      routes.push({
        route: route === '' ? '/' : route,
        file: path.relative('app', fullPath)
      });
    }
  }
  
  return routes;
}

console.log('🔍 Analyse des routes Next.js App Router:');
console.log('==========================================');

const routes = analyzeRoutes('app');
routes.sort((a, b) => a.route.localeCompare(b.route));

routes.forEach(({ route, file }) => {
  console.log(`${route.padEnd(30)} → ${file}`);
});

console.log('\n📋 Résumé:');
console.log(`Total des routes: ${routes.length}`);

// Détecter les conflits
const routeMap = {};
routes.forEach(({ route, file }) => {
  if (!routeMap[route]) {
    routeMap[route] = [];
  }
  routeMap[route].push(file);
});

const conflicts = Object.entries(routeMap).filter(([route, files]) => files.length > 1);
if (conflicts.length > 0) {
  console.log('\n⚠️ Conflits détectés:');
  conflicts.forEach(([route, files]) => {
    console.log(`${route}:`);
    files.forEach(file => console.log(`  - ${file}`));
  });
} else {
  console.log('\n✅ Aucun conflit détecté');
}
