# =============================================================================
# RWA Mining Ecosystem 2.0 - Docker Compose
# =============================================================================
#
# 支持两种部署模式 (通过 DEPLOY_MODE 环境变量切换):
#
# 1. shared 模式 (默认): 共享 1.0 的 PostgreSQL/Redis/Kafka
#    - 网络: services_rwa-network (由 1.0 创建)
#    - 基础设施: 复用 1.0 的 postgres, redis, kafka 容器
#
# 2. standalone 模式: 独立部署在单独服务器上
#    - 网络: rwa-2-network (本地)
#    - 基础设施: 本地 postgres-2, redis-2 + 远程 Kafka (192.168.1.111:9093)
#    - 启动命令需加 --profile standalone
#
# Usage:
#   # Shared 模式 (默认):
#   docker compose -f docker-compose.2.0.yml --env-file .env up -d
#
#   # Standalone 模式:
#   docker compose -f docker-compose.2.0.yml --profile standalone --env-file .env up -d
#
# =============================================================================

services:
  # ===========================================================================
  # Infrastructure (standalone 模式专属, 通过 --profile standalone 启用)
  # ===========================================================================

  postgres-2:
    image: postgres:16-alpine
    container_name: rwa-postgres-2
    profiles: ["standalone"]
    environment:
      TZ: Asia/Shanghai
      POSTGRES_USER: ${POSTGRES_USER:-rwa_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-rwa_secure_password}
      POSTGRES_MULTIPLE_DATABASES: rwa_contribution,rwa_mining,rwa_trading,rwa_mining_admin,rwa_auth,rwa_mining_wallet,rwa_mining_blockchain,rwa_mining_presence
    ports:
      # 安全加固: 仅绑定 127.0.0.1，禁止公网直连数据库
      # deploy-mining.sh 通过 docker exec 访问，不受影响
      - "127.0.0.1:5432:5432"
    volumes:
      - postgres_2_data:/var/lib/postgresql/data
      - ./init-multiple-dbs.sh:/docker-entrypoint-initdb.d/init-multiple-dbs.sh:ro
    command: >
      postgres
      -c wal_level=logical
      -c max_replication_slots=20
      -c max_wal_senders=20
      -c max_connections=200
      -c max_slot_wal_keep_size=10GB
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-rwa_user}"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped
    networks:
      - rwa-2-network

  redis-2:
    image: redis:7-alpine
    container_name: rwa-redis-2
    profiles: ["standalone"]
    environment:
      TZ: Asia/Shanghai
    command: redis-server --appendonly yes --databases 20
    ports:
      # 安全加固: 仅绑定 127.0.0.1，禁止公网直连 Redis
      # Redis 无密码保护，暴露公网极易被利用
      - "127.0.0.1:6379:6379"
    volumes:
      - redis_2_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped
    networks:
      - rwa-2-network

  debezium-2:
    # 必须锁定精确版本: 2.5.4.Final 修复 DBZ-7316 (WAL 积压 bug)
    image: debezium/connect:2.5.4.Final
    container_name: rwa-debezium-2
    profiles: ["standalone"]
    depends_on:
      postgres-2:
        condition: service_healthy
    ports:
      # 安全加固: 仅绑定 127.0.0.1，禁止公网访问 Debezium REST API
      # 此前 0.0.0.0:8084 暴露公网，已被注入 3 个恶意 connector (SSRF 攻击读取 /etc/passwd)
      # deploy-mining.sh 使用 http://localhost:8084 调用，绑定 127.0.0.1 后不受影响
      - "127.0.0.1:8084:8083"
    environment:
      TZ: Asia/Shanghai
      GROUP_ID: debezium-connect-2
      BOOTSTRAP_SERVERS: ${KAFKA_BROKERS:-kafka:29092}
      CONFIG_STORAGE_TOPIC: debezium_2_configs
      OFFSET_STORAGE_TOPIC: debezium_2_offsets
      STATUS_STORAGE_TOPIC: debezium_2_statuses
      CONFIG_STORAGE_REPLICATION_FACTOR: 1
      OFFSET_STORAGE_REPLICATION_FACTOR: 1
      STATUS_STORAGE_REPLICATION_FACTOR: 1
      # Offset 提交频率: 默认 60s, 缩短至 10s 以减少重启后重复处理窗口
      OFFSET_FLUSH_INTERVAL_MS: 10000
      OFFSET_FLUSH_TIMEOUT_MS: 5000
      KEY_CONVERTER: org.apache.kafka.connect.json.JsonConverter
      VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter
      KEY_CONVERTER_SCHEMAS_ENABLE: "false"
      VALUE_CONVERTER_SCHEMAS_ENABLE: "false"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8083/"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  # ===========================================================================
  # Backend Services (2.0)
  # ===========================================================================

  contribution-service:
    build:
      context: ./contribution-service
      dockerfile: Dockerfile
    container_name: rwa-contribution-service
    depends_on:
      postgres-2:
        condition: service_healthy
        required: false
      redis-2:
        condition: service_healthy
        required: false
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3020
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/rwa_contribution?schema=public
      # Redis - 使用 DB 10 隔离
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 10
      # Kafka - 消费 CDC 事件 (从1.0服务同步数据)
      KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}
      # 认种订单 (planting-service)
      CDC_TOPIC_ADOPTIONS: ${CDC_TOPIC_ADOPTIONS:-cdc.planting.public.planting_orders}
      # 推荐关系 (referral-service)
      CDC_TOPIC_REFERRALS: ${CDC_TOPIC_REFERRALS:-cdc.referral.public.referral_relationships}
      CDC_CONSUMER_GROUP: contribution-service-cdc-group
      # JWT 配置 (与 auth-service 共享密钥以验证 token)
      JWT_SECRET: ${JWT_SECRET:-your-jwt-secret-change-in-production}
    ports:
      - "3020:3020"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3020/api/v2/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  mining-service:
    build:
      context: ./mining-service
      dockerfile: Dockerfile
    container_name: rwa-mining-service
    depends_on:
      postgres-2:
        condition: service_healthy
        required: false
      redis-2:
        condition: service_healthy
        required: false
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3021
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/rwa_mining?schema=public
      # Redis - 使用 DB 16 隔离 (避免与 1.0 blockchain-service 的 DB 11 冲突)
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 16
      # Kafka
      KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}
      # JWT 配置 (与 auth-service 共享密钥以验证 token)
      JWT_SECRET: ${JWT_SECRET:-your-jwt-secret-change-in-production}
      # 2.0 内部服务调用
      CONTRIBUTION_SERVICE_URL: http://contribution-service:3020
    ports:
      - "3021:3021"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3021/api/v2/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  trading-service:
    build:
      context: ./trading-service
      dockerfile: Dockerfile
    container_name: rwa-trading-service
    depends_on:
      postgres-2:
        condition: service_healthy
        required: false
      redis-2:
        condition: service_healthy
        required: false
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3022
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/rwa_trading?schema=public
      # Redis - 使用 DB 12 隔离
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 12
      # Kafka
      KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}
      # 2.0 内部服务调用
      MINING_SERVICE_URL: http://mining-service:3021
      AUTH_SERVICE_URL: http://auth-service:3024
      MINING_ADMIN_SERVICE_URL: http://mining-admin-service:3023
      MINING_BLOCKCHAIN_SERVICE_URL: http://mining-blockchain-service:3026
      CONTRIBUTION_SERVICE_URL: http://contribution-service:3020
      # JWT 配置 (与 auth-service 共享密钥以验证 token)
      JWT_SECRET: ${JWT_SECRET:-your-jwt-secret-change-in-production}
      # 做市商钱包 (MPC) - 初始化时写入数据库
      FUSDT_MARKET_MAKER_USERNAME: ${FUSDT_MARKET_MAKER_USERNAME:-}
      FUSDT_MARKET_MAKER_ADDRESS: ${FUSDT_MARKET_MAKER_ADDRESS:-}
      EUSDT_MARKET_MAKER_USERNAME: ${EUSDT_MARKET_MAKER_USERNAME:-}
      EUSDT_MARKET_MAKER_ADDRESS: ${EUSDT_MARKET_MAKER_ADDRESS:-}
    volumes:
      - trading-uploads:/app/uploads
    ports:
      - "3022:3022"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3022/api/v2/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  mining-admin-service:
    build:
      context: ./mining-admin-service
      dockerfile: Dockerfile
    container_name: rwa-mining-admin-service
    depends_on:
      postgres-2:
        condition: service_healthy
        required: false
      redis-2:
        condition: service_healthy
        required: false
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3023
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/rwa_mining_admin?schema=public
      # Redis - 使用 DB 13 隔离
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 13
      # Kafka - 消费 2.0 服务间事件
      KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}
      CDC_CONSUMER_GROUP: mining-admin-service-cdc-group
      # CDC Topics - 从各 2.0 服务同步数据
      CDC_TOPIC_USERS: ${CDC_TOPIC_ADMIN_USERS:-mining-admin.auth.users}
      CDC_TOPIC_CONTRIBUTION: ${CDC_TOPIC_ADMIN_CONTRIBUTION:-mining-admin.contribution.accounts}
      CDC_TOPIC_MINING: ${CDC_TOPIC_ADMIN_MINING:-mining-admin.mining.accounts}
      CDC_TOPIC_TRADING: ${CDC_TOPIC_ADMIN_TRADING:-mining-admin.trading.accounts}
      # 2.0 内部服务调用（备用）
      CONTRIBUTION_SERVICE_URL: http://contribution-service:3020
      MINING_SERVICE_URL: http://mining-service:3021
      TRADING_SERVICE_URL: http://trading-service:3022
      AUTH_SERVICE_URL: http://auth-service:3024
      MINING_WALLET_SERVICE_URL: http://mining-wallet-service:3025
      MINING_BLOCKCHAIN_SERVICE_URL: http://mining-blockchain-service:3026
      # 池账户钱包名称 (MPC 用户名，用于 walletName → poolType 映射)
      BURN_POOL_WALLET_USERNAME: ${BURN_POOL_WALLET_USERNAME:-}
      MINING_POOL_WALLET_USERNAME: ${MINING_POOL_WALLET_USERNAME:-}
      JWT_SECRET: ${ADMIN_JWT_SECRET:-your-admin-jwt-secret-change-in-production}
      # APK 下载地址前缀（通过 Kong 网关 /mining-admin 路由访问）
      UPLOAD_BASE_URL: https://mapi.szaiai.com/mining-admin/downloads
      UPLOAD_DIR: ./uploads
    volumes:
      - mining-admin-uploads:/app/uploads
    ports:
      - "3023:3023"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3023/api/v2/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  auth-service:
    build:
      context: ./auth-service
      dockerfile: Dockerfile
    container_name: rwa-auth-service
    depends_on:
      postgres-2:
        condition: service_healthy
        required: false
      redis-2:
        condition: service_healthy
        required: false
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3024
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/rwa_auth?schema=public
      # Redis - 使用 DB 14 隔离
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 14
      # Kafka - 消费 CDC 事件 (从1.0 identity-service同步用户)
      KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}
      CDC_ENABLED: "true"
      CDC_TOPIC_USERS: ${CDC_TOPIC_USERS:-cdc.identity.public.user_accounts}
      CDC_TOPIC_WALLET_ADDRESSES: ${CDC_TOPIC_WALLET_ADDRESSES:-cdc.identity.public.wallet_addresses}
      CDC_CONSUMER_GROUP: auth-service-cdc-group
      # JWT 配置
      JWT_SECRET: ${JWT_SECRET:-your-jwt-secret-change-in-production}
      JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-7d}
      JWT_REFRESH_EXPIRES_IN: ${JWT_REFRESH_EXPIRES_IN:-30d}
      # SMS 配置 (阿里云)
      SMS_ENABLED: ${SMS_ENABLED:-false}
      ALIYUN_ACCESS_KEY_ID: ${ALIYUN_ACCESS_KEY_ID:-}
      ALIYUN_ACCESS_KEY_SECRET: ${ALIYUN_ACCESS_KEY_SECRET:-}
      ALIYUN_SMS_SIGN_NAME: ${ALIYUN_SMS_SIGN_NAME:-深圳果冻移民咨询}
      ALIYUN_SMS_TEMPLATE_CODE: ${ALIYUN_SMS_TEMPLATE_CODE:-}
      ALIYUN_SMS_ENDPOINT: ${ALIYUN_SMS_ENDPOINT:-dysmsapi.aliyuncs.com}
    ports:
      - "3024:3024"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3024/api/v2/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  mining-wallet-service:
    build:
      context: ./mining-wallet-service
      dockerfile: Dockerfile
    container_name: rwa-mining-wallet-service
    depends_on:
      postgres-2:
        condition: service_healthy
        required: false
      redis-2:
        condition: service_healthy
        required: false
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3025
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/rwa_mining_wallet?schema=public
      # Redis - 使用 DB 15 隔离
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 15
      # Kafka
      KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}
      # JWT 配置
      JWT_SECRET: ${JWT_SECRET:-your-jwt-secret-change-in-production}
      # KAVA 区块链配置
      KAVA_RPC_URL: ${KAVA_RPC_URL:-https://evm.kava.io}
      KAVA_CHAIN_ID: ${KAVA_CHAIN_ID:-2222}
    ports:
      - "3025:3025"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3025/api/v2/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  mining-blockchain-service:
    build:
      context: ./mining-blockchain-service
      dockerfile: Dockerfile
    container_name: rwa-mining-blockchain-service
    depends_on:
      postgres-2:
        condition: service_healthy
        required: false
      redis-2:
        condition: service_healthy
        required: false
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3026
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/rwa_mining_blockchain?schema=public
      # Redis - 使用 DB 8 隔离 (0-15 有效范围)
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 8
      # Kafka - 用于事件发布
      KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}
      # JWT 配置
      JWT_SECRET: ${JWT_SECRET:-your-jwt-secret-change-in-production}
      # MPC system 直调配置 (account-service port 4000)
      MPC_ACCOUNT_SERVICE_URL: ${MPC_ACCOUNT_SERVICE_URL:-http://localhost:4000}
      MPC_JWT_SECRET: ${MPC_JWT_SECRET:-}
      # 区块链配置
      NETWORK_MODE: ${NETWORK_MODE:-mainnet}
      # KAVA 配置
      KAVA_RPC_URL: ${KAVA_RPC_URL:-https://evm.kava.io}
      # RPC 故障转移：逗号分隔的多个端点，主端点失败 3 分钟后自动切换
      KAVA_RPC_URLS: ${KAVA_RPC_URLS:-https://evm.kava.io,https://evm.kava-rpc.com,https://kava-evm-rpc.publicnode.com,https://rpc.ankr.com/kava_evm}
      KAVA_CHAIN_ID: ${KAVA_CHAIN_ID:-2222}
      KAVA_USDT_CONTRACT: ${KAVA_USDT_CONTRACT:-0xA9F3A35dBa8699c8C681D8db03F0c1A8CEB9D7c3}
      # 积分股合约 (eUSDT - Energy USDT)
      KAVA_EUSDT_CONTRACT: ${KAVA_EUSDT_CONTRACT:-0x7C3275D808eFbAE90C06C7E3A9AfDdcAa8563931}
      # 积分值合约 (fUSDT - Future USDT)
      KAVA_FUSDT_CONTRACT: ${KAVA_FUSDT_CONTRACT:-0x14dc4f7d3E4197438d058C3D156dd9826A161134}
      # BSC 配置
      BSC_RPC_URL: ${BSC_RPC_URL:-https://bsc-dataseed.binance.org}
      BSC_CHAIN_ID: ${BSC_CHAIN_ID:-56}
      BSC_USDT_CONTRACT: ${BSC_USDT_CONTRACT:-0x55d398326f99059fF775485246999027B3197955}
      # C2C Bot 热钱包 (MPC)
      C2C_BOT_WALLET_USERNAME: ${C2C_BOT_WALLET_USERNAME:-wallet-29b09647}
      C2C_BOT_WALLET_ADDRESS: ${C2C_BOT_WALLET_ADDRESS:-0x30da9Eb4C49914fDb9d711cE26AB446d68fE4Bc4}
      # eUSDT (积分股) 做市商钱包 (MPC)
      EUSDT_MARKET_MAKER_USERNAME: ${EUSDT_MARKET_MAKER_USERNAME:-}
      EUSDT_MARKET_MAKER_ADDRESS: ${EUSDT_MARKET_MAKER_ADDRESS:-}
      # fUSDT (积分值) 做市商钱包 (MPC)
      FUSDT_MARKET_MAKER_USERNAME: ${FUSDT_MARKET_MAKER_USERNAME:-}
      FUSDT_MARKET_MAKER_ADDRESS: ${FUSDT_MARKET_MAKER_ADDRESS:-}
      # 池账户钱包 (MPC) - 100亿销毁池 + 200万挖矿池
      BURN_POOL_WALLET_USERNAME: ${BURN_POOL_WALLET_USERNAME:-}
      BURN_POOL_WALLET_ADDRESS: ${BURN_POOL_WALLET_ADDRESS:-}
      MINING_POOL_WALLET_USERNAME: ${MINING_POOL_WALLET_USERNAME:-}
      MINING_POOL_WALLET_ADDRESS: ${MINING_POOL_WALLET_ADDRESS:-}
      # fUSDT 注入钱包 (MPC) - 认种自动转积分值到做市商
      FUSDT_INJECTION_WALLET_USERNAME: ${FUSDT_INJECTION_WALLET_USERNAME:-}
      FUSDT_INJECTION_WALLET_ADDRESS: ${FUSDT_INJECTION_WALLET_ADDRESS:-}
      # CDC 主题配置（Debezium CDC outbox）
      CDC_TOPIC_CONTRIBUTION_OUTBOX: ${CDC_TOPIC_CONTRIBUTION_OUTBOX:-cdc.contribution.outbox}
      # 区块扫描配置
      BLOCK_SCAN_INTERVAL_MS: ${BLOCK_SCAN_INTERVAL_MS:-5000}
      BLOCK_CONFIRMATIONS_REQUIRED: ${BLOCK_CONFIRMATIONS_REQUIRED:-12}
    ports:
      - "3026:3026"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3026/api/v1/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  presence-service-2:
    build:
      context: ./presence-service
      dockerfile: Dockerfile
    container_name: rwa-presence-service-2
    depends_on:
      postgres-2:
        condition: service_healthy
        required: false
      redis-2:
        condition: service_healthy
        required: false
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3027
      APP_PORT: 3027
      # PostgreSQL - 2.0 独立的 presence 数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/rwa_mining_presence?schema=public
      # Redis - 使用 DB 17 隔离 (2.0 已用: 8,10,12,13,14,15,16)
      REDIS_HOST: ${REDIS_HOST:-redis}
      REDIS_PORT: ${REDIS_PORT:-6379}
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 17
      # Kafka - 发布 session-started 事件
      KAFKA_BROKERS: ${KAFKA_BROKERS:-kafka:29092}
      # JWT - 与 auth-service 共享密钥以验证 mining-app token
      JWT_SECRET: ${JWT_SECRET:-your-jwt-secret-change-in-production}
      ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET:-your-admin-jwt-secret-change-in-production}
      # 在线窗口: 180 秒无心跳视为离线
      PRESENCE_WINDOW_SECONDS: 180
      SNAPSHOT_INTERVAL_SECONDS: 60
    ports:
      - "3027:3027"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3027/api/v1/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    networks:
      - rwa-2-network

  # ===========================================================================
  # Frontend Services (2.0)
  # ===========================================================================

  mining-admin-web:
    build:
      context: ../../frontend/mining-admin-web
      dockerfile: Dockerfile
    container_name: rwa-mining-admin-web
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3100
      NEXT_PUBLIC_API_URL: http://mining-admin-service:3023
      NEXT_PUBLIC_APP_NAME: 挖矿管理后台
      # 直连后端服务 (同一 Docker 网络内, 不经 Kong 网关)
      # 如需走 Kong 网关, 设置 API_GATEWAY_URL 即可 (如 https://mapi.szaiai.com)
      TRADING_SERVICE_URL: http://trading-service:3022
      MINING_SERVICE_URL: http://mining-service:3021
    ports:
      - "3100:3100"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3100/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 15s
    restart: unless-stopped
    networks:
      - rwa-2-network

# =============================================================================
# Volumes
# =============================================================================
volumes:
  mining-admin-uploads:
    driver: local
  trading-uploads:
    driver: local
  # standalone 模式专属 volumes
  postgres_2_data:
    driver: local
  redis_2_data:
    driver: local

# =============================================================================
# Network
# =============================================================================
# shared 模式 (默认): RWA_NETWORK_NAME 未设置，使用 services_rwa-network (1.0 已创建)
# standalone 模式: deploy-mining.sh 设置 RWA_NETWORK_NAME=rwa-2-network 并预先创建
networks:
  rwa-2-network:
    external: true
    name: ${RWA_NETWORK_NAME:-services_rwa-network}
