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

const productionConfig = {
  // Server settings
  port: process.env.PORT || 3001,
  
  // Database settings
  database: {
    uri: process.env.MONGODB_URI || 'mongodb://localhost:27017/galaxystrikeonline',
    options: {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      maxPoolSize: 10,
      serverSelectionTimeoutMS: 5000,
      socketTimeoutMS: 45000,
    }
  },
  
  // JWT settings
  jwt: {
    secret: process.env.JWT_SECRET,
    expiresIn: '24h',
    refreshExpiresIn: '7d'
  },
  
  // Redis settings (for sessions and caching)
  redis: {
    url: process.env.REDIS_URL || 'redis://localhost:6379',
    options: {
      retryDelayOnFailover: 100,
      maxRetriesPerRequest: 3,
    }
  },
  
  // CORS settings
  cors: {
    origin: process.env.CLIENT_URL || 'http://localhost:3000',
    credentials: true,
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    allowedHeaders: ['Content-Type', 'Authorization']
  },
  
  // Rate limiting
  rateLimit: {
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100, // limit each IP to 100 requests per windowMs
    message: 'Too many requests from this IP, please try again later.',
    standardHeaders: true,
    legacyHeaders: false,
  },
  
  // Socket.IO settings
  socketio: {
    cors: {
      origin: process.env.CLIENT_URL || 'http://localhost:3000',
      methods: ['GET', 'POST']
    },
    pingTimeout: 60000,
    pingInterval: 25000,
    maxHttpBufferSize: 1e8, // 100 MB
  },
  
  // Logging settings
  logging: {
    level: process.env.LOG_LEVEL || 'info',
    format: process.env.NODE_ENV === 'production' ? 'json' : 'simple',
    file: {
      enabled: true,
      filename: 'logs/app.log',
      maxsize: 10485760, // 10MB
      maxFiles: 5,
    }
  },
  
  // Security settings
  security: {
    helmet: {
      contentSecurityPolicy: {
        directives: {
          defaultSrc: ["'self'"],
          styleSrc: ["'self'", "'unsafe-inline'"],
          scriptSrc: ["'self'"],
          imgSrc: ["'self'", "data:", "https:"],
        },
      },
      hsts: {
        maxAge: 31536000,
        includeSubDomains: true,
        preload: true
      }
    },
    compression: {
      level: 6,
      threshold: 1024,
    }
  },
  
  // Game settings
  game: {
    maxPlayersPerServer: 50,
    serverCleanupInterval: 300000, // 5 minutes
    inactivePlayerTimeout: 1800000, // 30 minutes
    autoSaveInterval: 60000, // 1 minute
  }
};

// Validate required environment variables
const validateConfig = () => {
  const required = ['JWT_SECRET'];
  const missing = required.filter(key => !process.env[key]);
  
  if (missing.length > 0) {
    logger.error(`Missing required environment variables: ${missing.join(', ')}`);
    process.exit(1);
  }
  
  if (process.env.NODE_ENV === 'production') {
    const prodRequired = ['MONGODB_URI', 'CLIENT_URL'];
    const prodMissing = prodRequired.filter(key => !process.env[key]);
    
    if (prodMissing.length > 0) {
      logger.error(`Missing required production environment variables: ${prodMissing.join(', ')}`);
      process.exit(1);
    }
  }
};

module.exports = {
  ...productionConfig,
  validateConfig
};
