version: '3.8'

services:
  postgres:
    image: timescale/timescaledb:latest-pg16
    environment:
      POSTGRES_USER: explorer
      POSTGRES_PASSWORD: ${DB_PASSWORD:-changeme}
      POSTGRES_DB: explorer
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U explorer"]
      interval: 10s
      timeout: 5s
      retries: 5

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - "9200:9200"
    volumes:
      - es_data:/usr/share/elasticsearch/data
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:9200/_cluster/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  indexer:
    build:
      context: ../backend
      dockerfile: Dockerfile.indexer
    environment:
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=explorer
      - DB_PASSWORD=${DB_PASSWORD:-changeme}
      - DB_NAME=explorer
      - RPC_URL=${RPC_URL:-http://localhost:8545}
      - WS_URL=${WS_URL:-ws://localhost:8546}
      - CHAIN_ID=138
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "explorer", "-h", "postgres"]
      interval: 30s
      timeout: 10s
      retries: 3
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2G
        reservations:
          cpus: '0.5'
          memory: 512M
    restart: unless-stopped
    labels:
      - "com.solacescanscout.name=indexer"
      - "com.solacescanscout.version=1.0.0"
      - "com.solacescanscout.service=block-indexer"

  api:
    build:
      context: ../backend
      dockerfile: Dockerfile.api
    environment:
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=explorer
      - DB_PASSWORD=${DB_PASSWORD:-changeme}
      - DB_NAME=explorer
      - PORT=8080
      - CHAIN_ID=138
      - REDIS_URL=redis://redis:6379
      # Optional relay health for mission-control / bridge UI (see backend CCIP_RELAY_HEALTH_URLS)
      - CCIP_RELAY_HEALTH_URL=${CCIP_RELAY_HEALTH_URL:-}
      - CCIP_RELAY_HEALTH_URLS=${CCIP_RELAY_HEALTH_URLS:-}
    ports:
      - "8080:8080"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2G
        reservations:
          cpus: '1'
          memory: 1G
    restart: unless-stopped
    labels:
      - "com.solacescanscout.name=api"
      - "com.solacescanscout.version=1.0.0"
      - "com.solacescanscout.service=rest-api"

  frontend:
    build:
      context: ../frontend
      dockerfile: Dockerfile
    environment:
      - NEXT_PUBLIC_API_URL=http://localhost:8080
      - NEXT_PUBLIC_CHAIN_ID=138
    ports:
      - "3000:3000"
    depends_on:
      api:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 1G
        reservations:
          cpus: '0.5'
          memory: 512M
    restart: unless-stopped
    labels:
      - "com.solacescanscout.name=frontend"
      - "com.solacescanscout.version=1.0.0"
      - "com.solacescanscout.service=web-frontend"

  virtual-banker-api:
    build:
      context: ../virtual-banker/backend
      dockerfile: ../virtual-banker/deployment/Dockerfile.backend
    environment:
      - DATABASE_URL=postgres://explorer:${DB_PASSWORD:-changeme}@postgres:5432/explorer?sslmode=disable
      - REDIS_URL=redis://redis:6379
      - PORT=8081
    ports:
      - "8081:8081"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8081/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2G
        reservations:
          cpus: '1'
          memory: 1G
    restart: unless-stopped
    labels:
      - "com.solacescanscout.name=virtual-banker-api"
      - "com.solacescanscout.version=1.0.0"
      - "com.solacescanscout.service=virtual-banker-api"

  virtual-banker-widget:
    build:
      context: ../virtual-banker/widget
      dockerfile: ../virtual-banker/deployment/Dockerfile.widget
    ports:
      - "8082:80"
    restart: unless-stopped
    labels:
      - "com.solacescanscout.name=virtual-banker-widget"
      - "com.solacescanscout.version=1.0.0"
      - "com.solacescanscout.service=virtual-banker-widget-cdn"

volumes:
  postgres_data:
  es_data:
  redis_data:

