# Synor DEX Ecosystem - Docker Compose
# Perpetual futures, Oracle, Liquidity Aggregator, and Trading Infrastructure
#
# Usage:
#   docker compose -f docker-compose.dex.yml up --build
#
# Services:
#   - contract-builder: Builds all WASM contracts
#   - oracle-service: Multi-source price aggregation
#   - perps-engine: Perpetual futures trading engine
#   - aggregator: Cross-chain liquidity routing
#   - dex-api: REST/WebSocket API for trading interfaces
#   - dex-redis: State cache and real-time pub/sub
#
# Ports:
#   17500 - Oracle API
#   17510 - Perps REST API
#   17511 - Perps WebSocket
#   17520 - Aggregator API
#   17530 - DEX API Gateway REST
#   17531 - DEX API Gateway WebSocket
#   17540 - Redis (internal)

services:
  # ==========================================================================
  # Contract Builder - Builds all WASM contracts
  # ==========================================================================
  contract-builder:
    build:
      context: .
      dockerfile: Dockerfile.contracts
    container_name: synor-contract-builder
    volumes:
      - contracts-output:/output
    command: >
      sh -c '
        echo "=== Building Synor Smart Contracts ==="
        cp -r /contracts-output/* /output/ 2>/dev/null || true
        echo "=== Contract Build Complete ==="
        ls -la /output/wasm/ 2>/dev/null || echo "WASM directory empty"
      '

  # ==========================================================================
  # Oracle Service - Price Feed Aggregation
  # ==========================================================================
  oracle-service:
    image: node:20-alpine
    container_name: synor-oracle-service
    hostname: oracle-service
    restart: unless-stopped
    working_dir: /app
    volumes:
      - ./docker/dex/oracle:/app:ro
      - contracts-output:/contracts:ro
    environment:
      - NODE_ENV=production
      - PORT=17500
      - PRICE_SOURCES=binance,coinbase,kraken
      - UPDATE_INTERVAL_MS=1000
      - STALE_THRESHOLD_MS=60000
      - SYNOR_RPC_URL=http://synor-seed1:17110
      - REDIS_URL=redis://dex-redis:6379
    ports:
      - "17500:17500"   # Oracle API
    networks:
      - synor-dex-net
    depends_on:
      contract-builder:
        condition: service_completed_successfully
      dex-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:17500/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s
    command: ["node", "index.js"]

  # ==========================================================================
  # Perpetuals Engine - Leveraged Trading (2x-100x)
  # ==========================================================================
  perps-engine:
    image: node:20-alpine
    container_name: synor-perps-engine
    hostname: perps-engine
    restart: unless-stopped
    working_dir: /app
    volumes:
      - ./docker/dex/perps:/app:ro
      - contracts-output:/contracts:ro
    environment:
      - NODE_ENV=production
      - PORT=17510
      - MIN_LEVERAGE=2
      - MAX_LEVERAGE=100
      - MAINTENANCE_MARGIN_BPS=50
      - LIQUIDATION_FEE_BPS=500
      - FUNDING_INTERVAL_HOURS=8
      - ORACLE_URL=http://oracle-service:17500
      - SYNOR_RPC_URL=http://synor-seed1:17110
      - REDIS_URL=redis://dex-redis:6379
    ports:
      - "17510:17510"   # Perps API
      - "17511:17511"   # Perps WebSocket
    networks:
      - synor-dex-net
    depends_on:
      oracle-service:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:17510/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s
    command: ["node", "index.js"]

  # ==========================================================================
  # Liquidity Aggregator - Cross-chain DEX Routing
  # ==========================================================================
  aggregator:
    image: node:20-alpine
    container_name: synor-aggregator
    hostname: aggregator
    restart: unless-stopped
    working_dir: /app
    volumes:
      - ./docker/dex/aggregator:/app:ro
      - contracts-output:/contracts:ro
    environment:
      - NODE_ENV=production
      - PORT=17520
      - AGGREGATION_FEE_BPS=5
      - MAX_SLIPPAGE_BPS=100
      - SOURCES=synor,osmosis,dydx
      - IBC_ENABLED=true
      - ORACLE_URL=http://oracle-service:17500
      - SYNOR_RPC_URL=http://synor-seed1:17110
      - REDIS_URL=redis://dex-redis:6379
    ports:
      - "17520:17520"   # Aggregator API
    networks:
      - synor-dex-net
    depends_on:
      oracle-service:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:17520/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s
    command: ["node", "index.js"]

  # ==========================================================================
  # DEX API Gateway - Unified Trading Interface
  # ==========================================================================
  dex-api:
    image: node:20-alpine
    container_name: synor-dex-api
    hostname: dex-api
    restart: unless-stopped
    working_dir: /app
    volumes:
      - ./docker/dex/api:/app:ro
      - contracts-output:/contracts:ro
    environment:
      - NODE_ENV=production
      - PORT=17530
      - WS_PORT=17531
      - ORACLE_URL=http://oracle-service:17500
      - PERPS_URL=http://perps-engine:17510
      - AGGREGATOR_URL=http://aggregator:17520
      - SYNOR_RPC_URL=http://synor-seed1:17110
      - REDIS_URL=redis://dex-redis:6379
      - CORS_ORIGINS=*
      - RATE_LIMIT_RPM=600
    ports:
      - "17530:17530"   # REST API
      - "17531:17531"   # WebSocket
    networks:
      - synor-dex-net
    depends_on:
      perps-engine:
        condition: service_healthy
      aggregator:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:17530/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s
    command: ["node", "index.js"]

  # ==========================================================================
  # Redis - State Cache & Pub/Sub
  # ==========================================================================
  dex-redis:
    image: redis:7-alpine
    container_name: synor-dex-redis
    hostname: dex-redis
    restart: unless-stopped
    command: redis-server --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
    ports:
      - "17540:6379"    # Redis port (remapped)
    networks:
      - synor-dex-net
    volumes:
      - dex-redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 15s
      timeout: 5s
      retries: 3

  # ==========================================================================
  # Price Feed Simulator (for testnet)
  # ==========================================================================
  price-simulator:
    image: node:20-alpine
    container_name: synor-price-simulator
    hostname: price-simulator
    restart: unless-stopped
    working_dir: /app
    volumes:
      - ./docker/dex/simulator:/app:ro
    environment:
      - NODE_ENV=development
      - REDIS_URL=redis://dex-redis:6379
      - UPDATE_INTERVAL_MS=500
      - VOLATILITY=0.001
      - INITIAL_BTC_PRICE=45000
      - INITIAL_ETH_PRICE=2500
      - INITIAL_SYNOR_PRICE=1.50
    networks:
      - synor-dex-net
    depends_on:
      dex-redis:
        condition: service_healthy
    profiles:
      - simulator
    command: ["node", "index.js"]

# =============================================================================
# Networks
# =============================================================================
networks:
  synor-dex-net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.24.0.0/16

# =============================================================================
# Volumes
# =============================================================================
volumes:
  contracts-output:
    driver: local
  dex-redis-data:
    driver: local
