// ecosystem.config.cjs
// This file is the standard way to configure applications for PM2.
// It allows us to define all the settings for our application in one place.
// The .cjs extension is required because the project's package.json has "type": "module".
//
// IMPORTANT: This file defines SEPARATE apps for production and test environments.
// Production apps: flyer-crawler-api, flyer-crawler-worker, flyer-crawler-analytics-worker
// Test apps: flyer-crawler-api-test, flyer-crawler-worker-test, flyer-crawler-analytics-worker-test
//
// Use ecosystem.config.test.cjs for test deployments (contains only test apps).
// Use this file (ecosystem.config.cjs) for production deployments.

// --- Environment Variable Validation ---
// NOTE: We only WARN about missing secrets, not exit.
// Calling process.exit(1) prevents PM2 from reading the apps array.
// The actual application will fail to start if secrets are missing,
// which PM2 will handle with its restart logic.
const requiredSecrets = ['DB_HOST', 'JWT_SECRET', 'GEMINI_API_KEY'];
const missingSecrets = requiredSecrets.filter(key => !process.env[key]);

if (missingSecrets.length > 0) {
  console.warn('\n[ecosystem.config.cjs] WARNING: The following environment variables are MISSING:');
  missingSecrets.forEach(key => console.warn(`   - ${key}`));
  console.warn('[ecosystem.config.cjs] The application may fail to start if these are required.\n');
} else {
  console.log('[ecosystem.config.cjs] Critical environment variables are present.');
}

// --- Shared Environment Variables ---
// Define common variables to reduce duplication and ensure consistency across apps.
const sharedEnv = {
  DB_HOST: process.env.DB_HOST,
  DB_USER: process.env.DB_USER,
  DB_PASSWORD: process.env.DB_PASSWORD,
  DB_NAME: process.env.DB_NAME,
  REDIS_URL: process.env.REDIS_URL,
  REDIS_PASSWORD: process.env.REDIS_PASSWORD,
  FRONTEND_URL: process.env.FRONTEND_URL,
  JWT_SECRET: process.env.JWT_SECRET,
  GEMINI_API_KEY: process.env.GEMINI_API_KEY,
  GOOGLE_MAPS_API_KEY: process.env.GOOGLE_MAPS_API_KEY,
  SMTP_HOST: process.env.SMTP_HOST,
  SMTP_PORT: process.env.SMTP_PORT,
  SMTP_SECURE: process.env.SMTP_SECURE,
  SMTP_USER: process.env.SMTP_USER,
  SMTP_PASS: process.env.SMTP_PASS,
  SMTP_FROM_EMAIL: process.env.SMTP_FROM_EMAIL,
  SENTRY_DSN: process.env.SENTRY_DSN,
  SENTRY_ENVIRONMENT: process.env.SENTRY_ENVIRONMENT,
  SENTRY_ENABLED: process.env.SENTRY_ENABLED,
};

module.exports = {
  apps: [
    // =========================================================================
    // PRODUCTION APPS
    // =========================================================================
    {
      // --- Production API Server ---
      name: 'flyer-crawler-api',
      script: './node_modules/.bin/tsx',
      args: 'server.ts',
      cwd: '/var/www/flyer-crawler.projectium.com',
      max_memory_restart: '500M',
      instances: 'max',
      exec_mode: 'cluster',
      kill_timeout: 5000,
      log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
      max_restarts: 40,
      exp_backoff_restart_delay: 100,
      min_uptime: '10s',
      env: {
        NODE_ENV: 'production',
        WORKER_LOCK_DURATION: '120000',
        ...sharedEnv,
      },
    },
    {
      // --- Production General Worker ---
      name: 'flyer-crawler-worker',
      script: './node_modules/.bin/tsx',
      args: 'src/services/worker.ts',
      cwd: '/var/www/flyer-crawler.projectium.com',
      max_memory_restart: '1G',
      kill_timeout: 10000,
      log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
      max_restarts: 40,
      exp_backoff_restart_delay: 100,
      min_uptime: '10s',
      env: {
        NODE_ENV: 'production',
        ...sharedEnv,
      },
    },
    {
      // --- Production Analytics Worker ---
      name: 'flyer-crawler-analytics-worker',
      script: './node_modules/.bin/tsx',
      args: 'src/services/worker.ts',
      cwd: '/var/www/flyer-crawler.projectium.com',
      max_memory_restart: '1G',
      kill_timeout: 10000,
      log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
      max_restarts: 40,
      exp_backoff_restart_delay: 100,
      min_uptime: '10s',
      env: {
        NODE_ENV: 'production',
        ...sharedEnv,
      },
    },
  ],
};
