name: ${COMPOSE_PROJECT_NAME:-iop-dev}

services:
  postgres:
    image: postgres:17-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${IOP_POSTGRES_USER:-iop}
      POSTGRES_PASSWORD: ${IOP_POSTGRES_PASSWORD:-iop_dev_password}
      POSTGRES_DB: ${IOP_POSTGRES_DB:-iop-control-plane-dev}
    ports:
      - "${IOP_POSTGRES_BIND:-127.0.0.1}:${IOP_POSTGRES_PORT:-15400}:5432"
    volumes:
      - control-plane-postgres:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U \"$${POSTGRES_USER}\" -d \"$${POSTGRES_DB}\""]
      interval: 10s
      timeout: 3s
      retries: 5

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    ports:
      - "${IOP_REDIS_BIND:-127.0.0.1}:${IOP_REDIS_PORT:-16300}:6379"
    volumes:
      - control-plane-redis:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5

  control-plane:
    build:
      context: ..
      dockerfile: iop/apps/control-plane/Dockerfile
    restart: unless-stopped
    environment:
      IOP_DATABASE_URL: "postgres://${IOP_POSTGRES_USER:-iop}:${IOP_POSTGRES_PASSWORD:-iop_dev_password}@postgres:5432/${IOP_POSTGRES_DB:-iop-control-plane-dev}?sslmode=disable"
      IOP_REDIS_URL: "redis://redis:6379/${IOP_REDIS_DB:-2}"
      IOP_REDIS_KEY_PREFIX: "${IOP_REDIS_KEY_PREFIX:-iop:control-plane:dev:}"
      IOP_METRICS_PORT: "${IOP_CONTROL_PLANE_METRICS_CONTAINER_PORT:-9093}"
    ports:
      - "${IOP_CONTROL_PLANE_HTTP_BIND:-0.0.0.0}:${IOP_CONTROL_PLANE_HTTP_PORT:-18000}:18000"
      - "${IOP_CONTROL_PLANE_WIRE_BIND:-0.0.0.0}:${IOP_CONTROL_PLANE_WIRE_PORT:-19080}:19080"
      - "${IOP_CONTROL_PLANE_EDGE_WIRE_BIND:-0.0.0.0}:${IOP_CONTROL_PLANE_EDGE_WIRE_PORT:-19081}:19081"
      - "${IOP_CONTROL_PLANE_METRICS_BIND:-127.0.0.1}:${IOP_CONTROL_PLANE_METRICS_PORT:-19100}:${IOP_CONTROL_PLANE_METRICS_CONTAINER_PORT:-9093}"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:18000/healthz"]
      interval: 10s
      timeout: 3s
      retries: 5

  edge:
    build:
      context: ..
      dockerfile: iop/apps/edge/Dockerfile
    restart: unless-stopped
    environment:
      IOP_EDGE_ID: "${IOP_EDGE_ID:-edge-compose}"
      IOP_EDGE_NAME: "${IOP_EDGE_NAME:-Compose Edge}"
      IOP_EDGE_LISTEN: "0.0.0.0:9090"
      IOP_EDGE_METRICS_CONTAINER_PORT: "${IOP_EDGE_METRICS_CONTAINER_PORT:-9092}"
      IOP_CONTROL_PLANE_EDGE_WIRE_ADDR: "control-plane:19081"
      IOP_EDGE_NODE_ID: "${IOP_EDGE_NODE_ID:-external-node}"
      IOP_EDGE_NODE_ALIAS: "${IOP_EDGE_NODE_ALIAS:-external-node}"
      IOP_EDGE_NODE_TOKEN: "${IOP_EDGE_NODE_TOKEN:-dev-node-token-change-me}"
    ports:
      - "${IOP_EDGE_NODE_BIND:-0.0.0.0}:${IOP_EDGE_NODE_PORT:-19090}:9090"
      - "${IOP_EDGE_METRICS_BIND:-127.0.0.1}:${IOP_EDGE_METRICS_PORT:-19092}:${IOP_EDGE_METRICS_CONTAINER_PORT:-9092}"
    depends_on:
      control-plane:
        condition: service_healthy

  web:
    build:
      context: ..
      dockerfile: iop/apps/client/Dockerfile
      args:
        IOP_CONTROL_PLANE_HTTP_URL: "${IOP_CONTROL_PLANE_HTTP_URL:-http://localhost:18000}"
        IOP_CONTROL_PLANE_WIRE_URL: "${IOP_CONTROL_PLANE_WIRE_URL:-ws://localhost:19080/client}"
    restart: unless-stopped
    ports:
      - "${IOP_WEB_BIND:-0.0.0.0}:${IOP_WEB_PORT:-13000}:3000"
    depends_on:
      control-plane:
        condition: service_healthy

  prometheus:
    image: ${IOP_PROMETHEUS_IMAGE:-prom/prometheus:latest}
    restart: unless-stopped
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
      - "--storage.tsdb.path=/prometheus"
      - "--storage.tsdb.retention.time=${IOP_PROMETHEUS_RETENTION_TIME:-400d}"
      - "--web.enable-lifecycle"
    ports:
      - "${IOP_PROMETHEUS_BIND:-127.0.0.1}:${IOP_PROMETHEUS_PORT:-19110}:9090"
    volumes:
      - ${IOP_PROMETHEUS_CONFIG:-./configs/prometheus/prometheus.yml}:/etc/prometheus/prometheus.yml:ro
      - control-plane-prometheus:/prometheus
    depends_on:
      control-plane:
        condition: service_healthy

  grafana:
    image: ${IOP_GRAFANA_IMAGE:-grafana/grafana-oss:latest}
    restart: unless-stopped
    environment:
      GF_SECURITY_ADMIN_USER: "${IOP_GRAFANA_ADMIN_USER:-admin}"
      GF_SECURITY_ADMIN_PASSWORD: "${IOP_GRAFANA_ADMIN_PASSWORD:-iop_dev_admin}"
    ports:
      - "${IOP_GRAFANA_BIND:-0.0.0.0}:${IOP_GRAFANA_PORT:-19120}:3000"
    volumes:
      - ./configs/grafana/provisioning:/etc/grafana/provisioning:ro
      - control-plane-grafana:/var/lib/grafana
    depends_on:
      prometheus:
        condition: service_started

volumes:
  control-plane-postgres:
  control-plane-redis:
  control-plane-prometheus:
  control-plane-grafana:

networks:
  default:
    name: ${IOP_COMPOSE_NETWORK:-iop-dev-net}
    ipam:
      config:
        - subnet: ${IOP_COMPOSE_SUBNET:-10.89.0.0/24}
