# Synor DEX Services - Docker Compose (Lightweight)
# DEX services without contract building (contracts built separately)
#
# Usage:
#   docker compose -f docker-compose.dex-services.yml up -d
#
# Ports:
#   17500 - Oracle API
#   17510 - Perps REST API
#   17520 - Aggregator API
#   17530 - DEX API Gateway
#   17540 - Redis

services:
  # ==========================================================================
  # Redis - State Cache & Pub/Sub (starts first)
  # ==========================================================================
  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"
    networks:
      - synor-dex-net
    volumes:
      - dex-redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3

  # ==========================================================================
  # 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
    environment:
      - NODE_ENV=production
      - PORT=17500
      - UPDATE_INTERVAL_MS=1000
      - STALE_THRESHOLD_MS=60000
    ports:
      - "17500:17500"
    networks:
      - synor-dex-net
    depends_on:
      dex-redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:17500/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1)).on('error', () => process.exit(1))"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    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
    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
    ports:
      - "17510:17510"
    networks:
      - synor-dex-net
    depends_on:
      oracle-service:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:17510/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1)).on('error', () => process.exit(1))"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    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
    environment:
      - NODE_ENV=production
      - PORT=17520
      - AGGREGATION_FEE_BPS=5
      - MAX_SLIPPAGE_BPS=100
      - ORACLE_URL=http://oracle-service:17500
    ports:
      - "17520:17520"
    networks:
      - synor-dex-net
    depends_on:
      oracle-service:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:17520/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1)).on('error', () => process.exit(1))"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    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
    environment:
      - NODE_ENV=production
      - PORT=17530
      - ORACLE_URL=http://oracle-service:17500
      - PERPS_URL=http://perps-engine:17510
      - AGGREGATOR_URL=http://aggregator:17520
    ports:
      - "17530:17530"
    networks:
      - synor-dex-net
    depends_on:
      perps-engine:
        condition: service_healthy
      aggregator:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:17530/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1)).on('error', () => process.exit(1))"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    command: ["node", "index.js"]

networks:
  synor-dex-net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.28.0.0/16

volumes:
  dex-redis-data:
    driver: local
