import app from '../src/app.js';
import swaggerSpec from '../src/config/swagger.js';
import apiRoutes from '../src/routes/api.js';

// Extract routes from api.js
const expressRoutes = [];
apiRoutes.stack.forEach((middleware) => {
  if (middleware.route) {
    const methods = Object.keys(middleware.route.methods).map(m => m.toUpperCase());
    expressRoutes.push({
      path: '/api/v1' + middleware.route.path,
      rawPath: middleware.route.path,
      methods: methods
    });
  }
});

console.log(`Total Express routes in api.js: ${expressRoutes.length}`);

// Extract paths from swaggerSpec
const swaggerPaths = swaggerSpec.paths || {};
const swaggerPathKeys = Object.keys(swaggerPaths).filter(p => p.startsWith('/'));
console.log(`Total Swagger paths defined: ${swaggerPathKeys.length}`);

// Compare
const expressPathSet = new Set(expressRoutes.map(r => r.rawPath));
const swaggerPathSet = new Set(swaggerPathKeys);

const inExpressNotInSwagger = [];
const inSwaggerNotInExpress = [];

expressPathSet.forEach(p => {
  const swaggerStylePath = p.replace(/:([a-zA-Z0-9_]+)/g, '{$1}');
  if (!swaggerPathSet.has(swaggerStylePath) && !swaggerPathSet.has(p)) {
    inExpressNotInSwagger.push({ express: p, swaggerStyle: swaggerStylePath });
  }
});

swaggerPathSet.forEach(p => {
  const expressStylePath = p.replace(/\{([a-zA-Z0-9_]+)\}/g, ':$1');
  if (!expressPathSet.has(expressStylePath) && !expressPathSet.has(p)) {
    inSwaggerNotInExpress.push(p);
  }
});

console.log('\n--- Express routes NOT in Swagger ---');
console.log(JSON.stringify(inExpressNotInSwagger, null, 2));

console.log('\n--- Swagger paths NOT in Express ---');
console.log(JSON.stringify(inSwaggerNotInExpress, null, 2));

console.log('\n--- All Express Routes List ---');
expressRoutes.forEach(r => {
  console.log(`${r.methods.join(', ')} /api/v1${r.rawPath}`);
});
