module.exports = {
  apps: [
    {
      name: 'pamsika-whatsapp-bot',
      script: './server.js',
      instances: 'max',
      exec_mode: 'cluster',

      // Environment variables
      env: {
        NODE_ENV: 'development',
        PORT: 3003,
        LOG_LEVEL: 'info'
      },

      env_production: {
        NODE_ENV: 'production',
        PORT: 3003,
        LOG_LEVEL: 'info'
      },

      env_staging: {
        NODE_ENV: 'staging',
        PORT: 3004,
        LOG_LEVEL: 'debug'
      },

      // PM2 Configuration
      watch: false,
      ignore_watch: ['node_modules', 'logs', 'sessions', 'users', '.git'],
      watch_options: {
        followSymlinks: false
      },

      // Restart configuration
      max_memory_restart: '1G',
      restart_delay: 4000,
      max_restarts: 10,
      min_uptime: '10s',

      // Clustering
      instance_var: 'INSTANCE_ID',

      // Logging
      log_file: './logs/pm2-combined.log',
      out_file: './logs/pm2-out.log',
      error_file: './logs/pm2-error.log',
      log_type: 'json',
      log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
      merge_logs: true,

      // Advanced features
      kill_timeout: 5000,
      wait_ready: true,
      listen_timeout: 10000,

      // Monitoring
      pmx: true,
      automation: false,

      // Source map support
      source_map_support: true,

      // Node.js arguments
      node_args: '--max-old-space-size=1024',

      // Application arguments
      args: '--color',

      // Cron restart (optional - restart every day at 3 AM)
      cron_restart: '0 3 * * *',

      // Environment-specific settings
      env_file: '.env'
    },

    // Background worker for message queue processing
    {
      name: 'pamsika-worker',
      script: './workers/message-worker.js',
      instances: 2,
      exec_mode: 'cluster',

      env: {
        NODE_ENV: 'development',
        WORKER_TYPE: 'message_queue'
      },

      env_production: {
        NODE_ENV: 'production',
        WORKER_TYPE: 'message_queue'
      },

      // Worker-specific settings
      max_memory_restart: '512M',
      restart_delay: 2000,
      max_restarts: 15,
      min_uptime: '5s',

      // Logging
      log_file: './logs/worker-combined.log',
      out_file: './logs/worker-out.log',
      error_file: './logs/worker-error.log',

      // No web server, so no port needed
      env_file: '.env'
    },

    // Cron job scheduler
    {
      name: 'pamsika-scheduler',
      script: './cron.js',
      instances: 1,
      exec_mode: 'fork',

      env: {
        NODE_ENV: 'development',
        SCHEDULER_ENABLED: 'true'
      },

      env_production: {
        NODE_ENV: 'production',
        SCHEDULER_ENABLED: 'true'
      },

      // Scheduler-specific settings
      max_memory_restart: '256M',
      restart_delay: 1000,
      max_restarts: 20,
      min_uptime: '30s',

      // Logging
      log_file: './logs/scheduler-combined.log',
      out_file: './logs/scheduler-out.log',
      error_file: './logs/scheduler-error.log',

      env_file: '.env'
    }
  ],

  // Deployment configuration
  deploy: {
    production: {
      user: 'deploy',
      host: ['your-production-server.com'],
      ref: 'origin/main',
      repo: 'git@github.com:pamsikaonline/whatsapp-bot.git',
      path: '/var/www/pamsika-whatsapp-bot',
      'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env production && pm2 save',
      'pre-setup': 'git clone https://github.com/pamsikaonline/whatsapp-bot.git /var/www/pamsika-whatsapp-bot',
      'post-setup': 'ls -la',
      env: {
        NODE_ENV: 'production'
      }
    },

    staging: {
      user: 'deploy',
      host: ['your-staging-server.com'],
      ref: 'origin/develop',
      repo: 'git@github.com:pamsikaonline/whatsapp-bot.git',
      path: '/var/www/pamsika-whatsapp-bot-staging',
      'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env staging && pm2 save',
      env: {
        NODE_ENV: 'staging'
      }
    }
  }
};
