function setupRouters(app) {
  console.log('⚙️ Caricamento routes...');

  const routers = [
    ['/', 'index_router'],
    ['/subscribe', 'subscribe_router'],
    ['/sendmsg', 'sendmsg_router'],
    ['/sendnotif', 'sendnotif_router'],
    ['/push', 'push_router'],
    ['/news', 'newsletter_router'],
    ['/booking', 'booking_router'],
    ['/dashboard', 'dashboard_router'],
    ['/event', 'myevent_router'],
    ['/email', 'email_router'],
    ['/todos', 'todos_router'],
    ['/test', 'test_router'],
    ['/projects', 'projects_router'],
    ['/users', 'users_router'],
    ['/reactions', 'reactions_router'],
    ['/mygroup', 'mygroups_router'],
    ['/circuit', 'circuits_router'],
    ['/account', 'accounts_router'],
    ['/iscritti_conacreis', 'iscrittiConacreis_router'],
    ['/report', 'report_router'],
    ['/site', 'site_router'],
    ['/admin', 'admin_router'],
    ['/products', 'products_router'],
    ['/myscraping', 'myscraping_router'],
    ['/catalogs', 'catalogs_router'],
    ['/cart', 'cart_router'],
    ['/orders', 'orders_router'],
    ['/city', 'city_router'],
    ['/myskills', 'myskills_router'],
    ['/mygoods', 'mygoods_router'],
    ['/mygen', 'mygen_router'],
    ['/aitools', 'aitools_router'],
    ['/apisqlsrv', 'articleRoutes'],
    ['/api', 'api_router'],
    ['/api2', 'api2_router'],
    ['/api/telegram', 'telegram_router'],
    ['/inviti', 'invitaAmicoRoutes'],
  ];

  routers.forEach(([path, file]) => {
    try {
      const router = require(`../router/${file}`);
      app.use(path, router);
    } catch (err) {
      console.error(`❌ Errore caricamento router ${file}:`, err.stack ? err.stack : err.message);
      return false;
    }
  });

  app.get('/api/health', (req, res) => {
    res.json({
      status: 'ok',
      timestamp: new Date().toISOString(),
      service: 'invita-amico-api',
    });
  });

  app.get('/health', (req, res) => {
    res.json({ status: 'ok', timestamp: new Date().toISOString() });
  });

  return true;
}

function setupMailchimpRoutes(app) {
  const querystring = require('querystring');
  const mailchimpClientId = process.env.MAILCHIMP_CLIENT_ID || 'xxxxxxxxxxxxxxxx';
  const redirectUri = process.env.MAILCHIMP_REDIRECT_URI || 'http://127.0.0.1:3000/mailchimp/auth/callback';

  app.get('/mailchimp/auth/authorize', (req, res) => {
    const url =
      'https://login.mailchimp.com/oauth2/authorize?' +
      querystring.stringify({
        response_type: 'code',
        client_id: mailchimpClientId,
        redirect_uri: redirectUri,
      });
    res.redirect(url);
  });
}

module.exports = { setupRouters, setupMailchimpRoutes };
