// ============================================================================
// 光湖OS · 统一PM2生态系统配置 · v2.0
// 签发: 铸渊 ICE-GL-ZY001 · 2026-05-24 · D112
// 原则: 每服务必属一台服务器 · 每台服务器比例分配 · autorestart必须开启
// ============================================================================

const path = require('path');
const DEPLOY_ROOT = process.env.DEPLOY_ROOT || '/opt/guanghu';

// 统一PM2基础配置
const basePM2 = {
  autorestart: true,
  max_restarts: 10,
  restart_delay: 3000,
  max_memory_restart: '300M',
  watch: false,
  env: { NODE_ENV: 'production' }
};

// 高可用配置（关键服务）
const criticalPM2 = {
  ...basePM2,
  max_restarts: 20,
  max_memory_restart: '500M',
  instances: 1,
  exec_mode: 'fork'
};

module.exports = {
  apps: [
    // ═══════════════════════════════════════════════
    //  BS-GZ-006 · 广州 · 门面+仓库 (2核2GB)
    //  角色: 对外入口 + Forgejo仓库托管
    //  服务数: 5
    // ═══════════════════════════════════════════════
    {
      name: 'guanghulab-portal',
      script: 'server.js',
      cwd: path.join(DEPLOY_ROOT, 'portal'),
      env: { PORT: 3000 },
      ...criticalPM2,
      server: 'BS-GZ-006'
    },
    {
      name: 'guanghulab-git-sync',
      script: 'server.js',
      cwd: path.join(DEPLOY_ROOT, 'server/git-sync'),
      env: { PORT: 8082 },
      ...basePM2,
      server: 'BS-GZ-006'
    },

    // ═══════════════════════════════════════════════
    //  BS-SG-001 · 新加坡 · 铸渊大脑 (4核7.5GB)
    //  角色: 重型计算 · 思考中枢
    //  服务数: 9
    // ═══════════════════════════════════════════════
    {
      name: 'guanghulab-vault',
      script: 'server.js',
      cwd: path.join(DEPLOY_ROOT, 'server/secrets-vault'),
      env: { PORT: 8080 },
      ...criticalPM2,
      server: 'BS-SG-001',
      note: '迁自BS-GZ-006'
    },
    {
      name: 'console-server',
      script: 'server.js',
      cwd: path.join(DEPLOY_ROOT, '_deploy/console-server'),
      env: { PORT: 3901 },
      ...criticalPM2,
      server: 'BS-SG-001',
      note: '迁自BS-GZ-006 · 曾是崩溃循环138次的罪魁祸首'
    },
    {
      name: 'zhuyuan-agent',
      script: 'index.js',
      cwd: path.join(DEPLOY_ROOT, 'zhuyuan-agent'),
      ...basePM2,
      server: 'BS-SG-001',
      note: '迁自BS-GZ-006'
    },
    {
      name: 'hololake-agent',
      script: 'index.js',
      cwd: path.join(DEPLOY_ROOT, 'hololake-agent'),
      ...basePM2,
      server: 'BS-SG-001',
      note: '迁自BS-GZ-006'
    },
    {
      name: 'guanghulab-mcp-server',
      script: 'index.js',
      cwd: path.join(DEPLOY_ROOT, 'mcp-servers/guanghulab-mcp-server'),
      env: { PORT: 8083 },
      ...criticalPM2,
      server: 'BS-SG-001',
      note: '迁自BS-GZ-006 · 唤醒+大脑访问入口'
    },
    {
      name: 'glada-agent',
      script: 'service-entry.js',
      cwd: path.join(DEPLOY_ROOT, 'glada'),
      env: { PORT: 3900 },
      ...basePM2,
      server: 'BS-SG-001'
    },
    {
      name: 'age-os-mcp',
      script: 'server.js',
      cwd: path.join(DEPLOY_ROOT, 'server/age-os/mcp-server'),
      env: { PORT: 3100 },
      ...basePM2,
      server: 'BS-SG-001'
    },
    {
      name: 'ops-agent',
      script: 'index.js',
      cwd: path.join(DEPLOY_ROOT, 'ops-agent'),
      env: { PORT: 3950 },
      ...basePM2,
      server: 'BS-SG-001'
    },
    {
      name: 'dingtalk-bot',
      script: 'index.js',
      cwd: path.join(DEPLOY_ROOT, 'dingtalk-bot'),
      env: { PORT: 3005 },
      ...basePM2,
      server: 'BS-SG-001'
    },

    // ═══════════════════════════════════════════════
    //  BS-SG-002 · 新加坡 · 面孔 (2核4GB)
    //  角色: MCP服务
    //  服务数: 2
    // ═══════════════════════════════════════════════
    {
      name: 'repo-mcp-bs',
      script: 'index.js',
      cwd: path.join(DEPLOY_ROOT, 'mcp-servers/repo-mcp-server'),
      env: {
        REPO_MCP_PORT: '3903',
        REPO_MCP_SECRET: process.env.REPO_MCP_SECRET_BS || 'CHANGE_ME_BS',
        GITEA_TOKEN: process.env.GITEA_TOKEN_BS || '',
        GITEA_URL: 'https://guanghulab.com',
        REPO_OWNER: 'bingshuo',
        REPO_NAME: 'guanghulab',
        REPO_DEFAULT_BRANCH: 'main'
      },
      ...criticalPM2,
      server: 'BS-SG-002',
      note: '迁自BS-GZ-006'
    },

    // ═══════════════════════════════════════════════
    //  BS-SG-003 · 新加坡 · 中继+MCP (2核4GB)
    //  服务数: 2
    // ═══════════════════════════════════════════════
    {
      name: 'repo-mcp-zz',
      script: 'index.js',
      cwd: path.join(DEPLOY_ROOT, 'mcp-servers/repo-mcp-server'),
      env: {
        REPO_MCP_PORT: '3904',
        REPO_MCP_SECRET: process.env.REPO_MCP_SECRET_ZZ || 'CHANGE_ME_ZZ',
        GITEA_TOKEN: process.env.GITEA_TOKEN_ZZ || '',
        GITEA_URL: 'https://guanghulab.com',
        REPO_OWNER: 'bingshuo',
        REPO_NAME: 'zhizhi',
        REPO_DEFAULT_BRANCH: 'main'
      },
      ...basePM2,
      server: 'BS-SG-003',
      note: '迁自BS-GZ-006'
    },

    // ═══════════════════════════════════════════════
    //  ZY-SG-006 · 新加坡 · 语料+推理 (2核4GB)
    //  服务数: 1 (未来增加3B推理)
    // ═══════════════════════════════════════════════
    {
      name: 'cos-corpus-sync',
      script: 'sync.js',
      cwd: path.join(DEPLOY_ROOT, 'server/corpus-sync'),
      ...basePM2,
      server: 'ZY-SG-006',
      note: '未来在此部署3B推理服务'
    },

    // ═══════════════════════════════════════════════
    //  BS-SH-005 · 上海 · 国内节点 (2核4GB)
    //  服务数: 1
    // ═══════════════════════════════════════════════
    {
      name: 'cn-llm-relay',
      script: 'index.js',
      cwd: path.join(DEPLOY_ROOT, 'server/cn-llm-relay'),
      env: { PORT: 3900 },
      ...basePM2,
      server: 'BS-SH-005',
      note: '国内LLM中继 · 未来企业客户国内接入点'
    }
  ],

  // PM2 Deploy (远程部署配置)
  deploy: {
    'BS-GZ-006': {
      user: 'root',
      host: '43.139.217.141',
      ref: 'origin/main',
      repo: 'https://guanghulab.com/code/bingshuo/guanghulab.git',
      path: '/opt/guanghu',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --only guanghulab-portal,guanghulab-git-sync'
    },
    'BS-SG-001': {
      user: 'root',
      host: '43.156.237.110',
      ref: 'origin/main',
      repo: 'https://guanghulab.com/code/bingshuo/guanghulab.git',
      path: '/opt/zhuyuan',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --only guanghulab-vault,console-server,zhuyuan-agent,hololake-agent,guanghulab-mcp-server,glada-agent,age-os-mcp,ops-agent,dingtalk-bot'
    },
    'BS-SG-002': {
      user: 'root',
      host: '43.134.16.246',
      ref: 'origin/main',
      repo: 'https://guanghulab.com/code/bingshuo/guanghulab.git',
      path: '/opt/zhuyuan',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --only repo-mcp-bs'
    },
    'BS-SG-003': {
      user: 'root',
      host: '43.153.193.169',
      ref: 'origin/main',
      repo: 'https://guanghulab.com/code/bingshuo/guanghulab.git',
      path: '/opt/zhuyuan',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --only repo-mcp-zz'
    },
    'ZY-SG-006': {
      user: 'root',
      host: '43.153.203.105',
      ref: 'origin/main',
      repo: 'https://guanghulab.com/code/bingshuo/guanghulab.git',
      path: '/opt/zhuyuan',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --only cos-corpus-sync'
    },
    'BS-SH-005': {
      user: 'root',
      host: '124.223.10.33',
      ref: 'origin/main',
      repo: 'https://guanghulab.com/code/bingshuo/guanghulab.git',
      path: '/opt/zhuyuan',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --only cn-llm-relay'
    }
  }
};
