version: '3.8'

services:
  factoryz-labs-api:
    image: ghcr.io/afrhone/factoryz-labs:latest
    deploy:
      replicas: 2
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
      resources:
        limits:
          cpus: '1.0'
          memory: 2048M
        reservations:
          cpus: '0.5'
          memory: 1024M
      update_config:
        parallelism: 1
        delay: 10s
        order: start-first
      rollback_config:
        parallelism: 1
        delay: 5s
        order: stop-first
    networks:
      - hypergraph_overlay
    environment:
      - NODE_ENV=production
      - PORT=3001
      - API_BASE=/api/factoryz
      - DATABASE_URL=postgresql://factoryz:password@postgres:5432/factoryz
      - FACTORYZ_LABS_ENV=production
      - FACTORYZ_LABS_DEBUG=false
      - FACTORYZ_LABS_LOG_LEVEL=info
      - FACTORYZ_LABS_METRICS_ENABLED=true
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3001/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    ports:
      - "3001:3001"
    volumes:
      - factoryz-labs-data:/app/data
    secrets:
      - factoryz-labs-secret

  factoryz-labs-web:
    image: ghcr.io/afrhone/factoryz-labs-web:latest
    deploy:
      replicas: 2
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
      resources:
        limits:
          cpus: '0.5'
          memory: 1024M
        reservations:
          cpus: '0.25'
          memory: 512M
      update_config:
        parallelism: 1
        delay: 10s
        order: start-first
      rollback_config:
        parallelism: 1
        delay: 5s
        order: stop-first
    networks:
      - hypergraph_overlay
    environment:
      - NODE_ENV=production
      - PORT=3002
      - API_BASE_URL=http://factoryz-labs-api:3001
      - WS_BASE_URL=ws://factoryz-labs-api:3001
      - FACTORYZ_LABS_ENV=production
      - FACTORYZ_LABS_DEBUG=false
      - FACTORYZ_LABS_LOG_LEVEL=info
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3002/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    ports:
      - "3002:3002"

  factoryz-labs-monitor:
    image: prom/prometheus:latest
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
    networks:
      - hypergraph_overlay
    ports:
      - "9091:9090"
    volumes:
      - factoryz-labs-metrics:/prometheus
      - ./configs/factoryz-labs/prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.console.libraries=/etc/prometheus/console_libraries'

  factoryz-labs-grafana:
    image: grafana/grafana:latest
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
    networks:
      - hypergraph_overlay
    ports:
      - "3003:3000"
    volumes:
      - factoryz-labs-grafana:/var/lib/grafana
      - ./configs/factoryz-labs/grafana-datasources.yml:/etc/grafana/provisioning/datasources/datasources.yml
      - ./configs/factoryz-labs/grafana-dashboards.yml:/etc/grafana/provisioning/dashboards/dashboards.yml
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false

networks:
  hypergraph_overlay:
    external: true
    name: hypergraph_overlay

volumes:
  factoryz-labs-data:
    driver: local
  factoryz-labs-metrics:
    driver: local
  factoryz-labs-grafana:
    driver: local

secrets:
  factoryz-labs-secret:
    external: true