# Docker Compose for Synor Economics Services
# Phase 12: Economics & Billing Infrastructure

version: "3.8"

services:
  # Price Oracle Service
  # Aggregates SYNOR/USD prices from multiple sources
  price-oracle:
    build:
      context: .
      dockerfile: docker/economics-service/Dockerfile
    container_name: synor-price-oracle
    ports:
      - "4010:4010"  # HTTP API
      - "4011:4011"  # Metrics
    environment:
      - RUST_LOG=info
      - SERVICE_TYPE=oracle
      - ORACLE_UPDATE_INTERVAL=30
      - ORACLE_MIN_SOURCES=2
    volumes:
      - ./docker/economics-service/config.toml:/app/config/config.toml:ro
      - oracle-data:/app/data
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4010/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped
    networks:
      - synor-economics

  # Metering Service
  # Real-time usage tracking for L2 services
  metering-service:
    build:
      context: .
      dockerfile: docker/economics-service/Dockerfile
    container_name: synor-metering
    ports:
      - "4012:4010"  # HTTP API (different host port)
      - "4013:4011"  # Metrics
    environment:
      - RUST_LOG=info
      - SERVICE_TYPE=metering
      - METERING_BUFFER_SIZE=10000
      - METERING_AGGREGATION_INTERVAL=3600
    volumes:
      - ./docker/economics-service/config.toml:/app/config/config.toml:ro
      - metering-data:/app/data
    depends_on:
      - price-oracle
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4010/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped
    networks:
      - synor-economics

  # Billing Service
  # Invoice generation and payment processing
  billing-service:
    build:
      context: .
      dockerfile: docker/economics-service/Dockerfile
    container_name: synor-billing
    ports:
      - "4014:4010"  # HTTP API
      - "4015:4011"  # Metrics
    environment:
      - RUST_LOG=info
      - SERVICE_TYPE=billing
      - BILLING_CYCLE_DAYS=30
      - AUTO_PAY_ENABLED=true
    volumes:
      - ./docker/economics-service/config.toml:/app/config/config.toml:ro
      - billing-data:/app/data
    depends_on:
      - price-oracle
      - metering-service
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4010/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped
    networks:
      - synor-economics

  # Cost Calculator API
  # Cost estimation endpoint
  cost-calculator:
    build:
      context: .
      dockerfile: docker/economics-service/Dockerfile
    container_name: synor-cost-calculator
    ports:
      - "4016:4010"  # HTTP API
      - "4017:4011"  # Metrics
    environment:
      - RUST_LOG=info
      - SERVICE_TYPE=calculator
    volumes:
      - ./docker/economics-service/config.toml:/app/config/config.toml:ro
    depends_on:
      - price-oracle
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4010/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped
    networks:
      - synor-economics

  # Redis for caching and event streaming
  redis:
    image: redis:7-alpine
    container_name: synor-economics-redis
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    networks:
      - synor-economics

  # PostgreSQL for persistent storage
  postgres:
    image: postgres:16-alpine
    container_name: synor-economics-postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=synor
      - POSTGRES_PASSWORD=synor_economics_dev
      - POSTGRES_DB=synor_economics
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./docker/economics-service/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U synor -d synor_economics"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    networks:
      - synor-economics

networks:
  synor-economics:
    driver: bridge
    name: synor-economics-network

volumes:
  oracle-data:
  metering-data:
  billing-data:
  redis-data:
  postgres-data:
