module.exports = {
  apps: [
    {
      name: 'luckychit-api', // Match your current PM2 process name
      script: './src/server.js', // Correct path to server.js
      instances: 'max', // Use all CPU cores
      exec_mode: 'cluster', // Cluster mode for load balancing
      autorestart: true,
      watch: false, // Disable in production
      max_memory_restart: '1G',
      env: {
        NODE_ENV: 'development',
        PORT: 3000
      },
      env_production: {
        NODE_ENV: 'production',
        PORT: 3000
      },
      error_file: './logs/err.log',
      out_file: './logs/out.log',
      log_file: './logs/combined.log',
      time: true,
      merge_logs: true,
      max_restarts: 10,
      min_uptime: '10s',
      listen_timeout: 3000,
      kill_timeout: 5000,
      // Cron restart (optional - restart daily at 3 AM)
      cron_restart: '0 3 * * *',
      // Enable source maps for better error tracking
      node_args: '--enable-source-maps',
    }
  ],

  // Deployment configuration (optional)
  deploy: {
    production: {
      user: 'your-server-user',
      host: 'your-server-ip',
      ref: 'origin/main',
      repo: 'git@github.com:yourusername/chitfund.git',
      path: '/var/www/luckychit',
      'pre-deploy-local': '',
      'post-deploy': 'cd backend && npm install && pm2 reload ecosystem.config.js --env production',
      'pre-setup': ''
    }
  }
};

