// PM2 Ecosystem Configuration for Vision Videoke Subdirectory Deployment
// This configuration runs the Node.js backend on port 3001 to avoid conflicts

module.exports = {
  apps: [{
    name: 'visionvideoke-backend',
    script: 'server.js', // Your Node.js backend entry point
    cwd: '/var/www/html/music',
    instances: 'max',
    exec_mode: 'cluster',
    
    // Environment configuration
    env: {
      NODE_ENV: 'production',
      PORT: 3001, // Different port to avoid conflicts with main site
      SUBDIRECTORY_PATH: '/music',
      API_BASE_PATH: '/music/api',
      PUBLIC_PATH: '/music',
      
      // Application URLs
      APP_URL: 'https://yourdomain.com/music',
      API_URL: 'https://yourdomain.com/music/api',
      
      // API Keys (will be loaded from .env file)
      OPENAI_API_KEY: process.env.OPENAI_API_KEY,
      UDIO_API_KEY: process.env.UDIO_API_KEY,
      ELEVENLABS_API_KEY: process.env.ELEVENLABS_API_KEY,
      VEO_API_KEY: process.env.VEO_API_KEY,
      
      // Database configuration
      DATABASE_URL: process.env.DATABASE_URL,
      
      // File storage
      UPLOAD_DIR: '/var/www/html/music/uploads',
      TEMP_DIR: '/var/www/html/music/temp',
      
      // Security
      JWT_SECRET: process.env.JWT_SECRET,
      ENCRYPTION_KEY: process.env.ENCRYPTION_KEY,
      
      // CORS configuration for subdirectory
      CORS_ORIGIN: 'https://yourdomain.com',
      CORS_CREDENTIALS: 'true'
    },
    
    // Development environment
    env_development: {
      NODE_ENV: 'development',
      PORT: 3001,
      APP_URL: 'http://localhost:3000/music',
      API_URL: 'http://localhost:3001/api',
      CORS_ORIGIN: 'http://localhost:3000'
    },
    
    // Logging configuration
    error_file: '/var/log/visionvideoke/error.log',
    out_file: '/var/log/visionvideoke/out.log',
    log_file: '/var/log/visionvideoke/combined.log',
    time: true,
    
    // Performance settings
    max_memory_restart: '1G',
    node_args: '--max-old-space-size=1024',
    
    // Monitoring and restart settings
    watch: false,
    ignore_watch: ['node_modules', 'logs', 'uploads', 'temp'],
    restart_delay: 4000,
    max_restarts: 10,
    min_uptime: '10s',
    
    // Health monitoring
    health_check_grace_period: 3000,
    health_check_fatal_exceptions: true,
    
    // Auto restart on file changes (disable in production)
    autorestart: true,
    
    // Merge logs from all instances
    merge_logs: true,
    
    // Log rotation
    log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
    
    // Process management
    kill_timeout: 5000,
    listen_timeout: 3000,
    
    // Environment file
    env_file: '/var/www/html/music/.env'
  }],
  
  // Deployment configuration
  deploy: {
    production: {
      user: 'ec2-user',
      host: 'your-server-ip',
      ref: 'origin/main',
      repo: 'https://github.com/your-repo/vision-videoke.git',
      path: '/var/www/html/music',
      'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env production',
      'pre-setup': 'mkdir -p /var/www/html/music/logs /var/www/html/music/uploads /var/www/html/music/temp'
    },
    
    staging: {
      user: 'ec2-user',
      host: 'your-staging-server-ip',
      ref: 'origin/develop',
      repo: 'https://github.com/your-repo/vision-videoke.git',
      path: '/var/www/html/music-staging',
      'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env staging'
    }
  }
};

