require('./config/config');
const express = require('express');
const { connectToDatabase, connectionUrl, options } = require('./db/mongoose');
const { startServer } = require('./server/startServer');
const { runStartupTasks } = require('./server/init');

const tools = require('./tools/general');

const app = express();

connectToDatabase(connectionUrl, options)
  .then(async () => {
    console.log('✅ DB connesso');
    await startServer(app, process.env.PORT);
    await runStartupTasks(); // 👈 QUI richiami quello che prima chiamavi "mystart"
  })
  .catch((err) => {
    console.error('❌ Connessione DB fallita:', err);
    process.exit(1);
  });

module.exports = { app };
