/* nucleo-bot ― index.js */
import express  from 'express';
import morgan   from 'morgan';
import { config }           from './config.js';
import { log }              from './logger.js';
import { router }           from './routes.js';
import {
  waitForGateway,
  clearWebhooks,
  registerWebhook
} from './whatsapp.js';



export let globalMemory = {};

/* 🌐  Express app */
const app = express();
app.use(express.json({ limit: '1mb' }));
app.use(morgan('[:date[iso]] :method :url :status - :response-time ms'));
app.use(router);

/* 🚀  Bootstrap */
async function bootstrap() {
  await waitForGateway();
  await clearWebhooks();
  await registerWebhook();
}

/* 🏁  Arranque */
app.listen(config.PORT, () => {
  log('info', `🪪  Versión del bot: ${config.VERSION}`);
  log('info', `🚀  nucleo-bot escuchando en :${config.PORT}`);
  bootstrap().catch(e => log('error', 'Error en bootstrap:', e.message));
});
