const logger = require('./logger');
const sequelize = require('../config/database');
const { redisClient } = require('../config/redis');

async function gracefulShutdown(server) {
  try {
    logger.info('Initiating graceful shutdown...');

    server.close(() => {
      logger.info('HTTP server closed');
    });

    await Promise.all([
      sequelize.close(),
      redisClient.quit()
    ]);

    logger.info('All connections closed successfully');
    process.exit(0);
  } catch (error) {
    logger.error('Error during shutdown:', { error: error.stack });
    process.exit(1);
  }
}