const mongoose = require('mongoose');
const logger = require('../utils/logger');

const connectDB = async () => {
  try {
    console.log('[DATABASE] Connecting to MongoDB:', process.env.MONGODB_URI);
    const conn = await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/galaxystrikeonline');
    console.log(`[DATABASE] MongoDB Connected: ${conn.connection.host}`);
    logger.info(`MongoDB Connected: ${conn.connection.host}`);
  } catch (error) {
    console.error('[DATABASE] Database connection failed:', error);
    logger.error('Database connection failed:', error);
    process.exit(1);
  }
};

module.exports = connectDB;
