# =============================================================================
# RWA Mining Ecosystem 2.0 - Docker Compose
# =============================================================================
#
# 2.0 系统共享 1.0 的基础设施 (PostgreSQL, Redis, Kafka)，但完全隔离：
#   - 数据库：使用独立的数据库名称 (rwa_contribution, rwa_mining, etc.)
#   - Redis：使用独立的 DB 分区 (DB 10-15)
#   - Kafka：仅通过 CDC 消费 1.0 的数据变更事件，单向同步
#   - HTTP：2.0 服务之间互相调用，不调用 1.0 服务
#
# Usage:
#   docker-compose -f docker-compose.2.0.yml up -d              # 启动所有 2.0 服务
#   docker-compose -f docker-compose.2.0.yml down               # 停止所有 2.0 服务
#   docker-compose -f docker-compose.2.0.yml up -d mining-admin # 启动单个服务
#   docker-compose -f docker-compose.2.0.yml logs -f            # 查看日志
#
# =============================================================================

services:
  # ===========================================================================
  # Backend Services (2.0)
  # ===========================================================================

  contribution-service:
    build:
      context: ./contribution-service
      dockerfile: Dockerfile
    container_name: rwa-contribution-service
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3020
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/rwa_contribution?schema=public
      # Redis - 使用 DB 10 隔离
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 10
      # Kafka - 消费 CDC 事件 (从1.0服务同步数据)
      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-network

  mining-service:
    build:
      context: ./mining-service
      dockerfile: Dockerfile
    container_name: rwa-mining-service
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3021
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/rwa_mining?schema=public
      # Redis - 使用 DB 11 隔离
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 11
      # Kafka
      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-network

  trading-service:
    build:
      context: ./trading-service
      dockerfile: Dockerfile
    container_name: rwa-trading-service
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3022
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/rwa_trading?schema=public
      # Redis - 使用 DB 12 隔离
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 12
      # Kafka
      KAFKA_BROKERS: kafka:29092
      # 2.0 内部服务调用
      MINING_SERVICE_URL: http://mining-service:3021
      # JWT 配置 (与 auth-service 共享密钥以验证 token)
      JWT_SECRET: ${JWT_SECRET:-your-jwt-secret-change-in-production}
    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-network

  mining-admin-service:
    build:
      context: ./mining-admin-service
      dockerfile: Dockerfile
    container_name: rwa-mining-admin-service
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3023
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/rwa_mining_admin?schema=public
      # Redis - 使用 DB 13 隔离
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 13
      # Kafka - 消费 2.0 服务间事件
      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
      JWT_SECRET: ${ADMIN_JWT_SECRET:-your-admin-jwt-secret-change-in-production}
    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-network

  auth-service:
    build:
      context: ./auth-service
      dockerfile: Dockerfile
    container_name: rwa-auth-service
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3024
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/rwa_auth?schema=public
      # Redis - 使用 DB 14 隔离
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 14
      # Kafka - 消费 CDC 事件 (从1.0 identity-service同步用户)
      KAFKA_BROKERS: kafka:29092
      CDC_ENABLED: "true"
      CDC_TOPIC_USERS: ${CDC_TOPIC_USERS:-cdc.identity.public.user_accounts}
      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_ACCESS_KEY_ID: ${SMS_ACCESS_KEY_ID:-}
      SMS_ACCESS_KEY_SECRET: ${SMS_ACCESS_KEY_SECRET:-}
      SMS_SIGN_NAME: ${SMS_SIGN_NAME:-榴莲生态}
      SMS_TEMPLATE_CODE: ${SMS_TEMPLATE_CODE:-}
    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-network

  mining-wallet-service:
    build:
      context: ./mining-wallet-service
      dockerfile: Dockerfile
    container_name: rwa-mining-wallet-service
    environment:
      NODE_ENV: production
      TZ: Asia/Shanghai
      PORT: 3025
      # PostgreSQL - 使用独立的数据库
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/rwa_mining_wallet?schema=public
      # Redis - 使用 DB 15 隔离
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
      REDIS_DB: 15
      # Kafka
      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-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: 挖矿管理后台
    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-network

# =============================================================================
# 网络配置 - 连接到 1.0 的网络以共享基础设施
# =============================================================================
networks:
  rwa-network:
    external: true
    name: services_rwa-network
