services:
  postgres:
    image: postgres:15
    restart: unless-stopped
    environment:
      POSTGRES_DB: tuist
      POSTGRES_USER: tuist
      POSTGRES_PASSWORD: tuist
    ports:
      - "${POSTGRES_PORT:-5433}:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U tuist -d tuist"]
      interval: 5s
      timeout: 5s
      retries: 20

  pgweb:
    image: sosedoff/pgweb:0.16.2
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      PGWEB_DATABASE_URL: postgres://tuist:tuist@postgres:5432/tuist?sslmode=disable
    ports:
      - "${PGWEB_PORT:-8081}:8081"

  clickhouse-keeper:
    image: clickhouse/clickhouse-server:26.5-alpine
    restart: unless-stopped
    command: ["clickhouse-keeper", "--config-file=/etc/clickhouse-keeper/keeper_config.xml"]
    volumes:
      - ./clickhouse-keeper-config.xml:/etc/clickhouse-keeper/keeper_config.xml:ro
      - clickhouse-keeper-data:/var/lib/clickhouse
    healthcheck:
      test: ["CMD-SHELL", "clickhouse-keeper-client --host 127.0.0.1 --port 9181 --query ruok | grep -q imok"]
      interval: 5s
      timeout: 5s
      retries: 20

  clickhouse:
    image: clickhouse/clickhouse-server:26.5-alpine
    restart: unless-stopped
    depends_on:
      clickhouse-keeper:
        condition: service_healthy
    environment:
      CLICKHOUSE_DB: tuist
      CLICKHOUSE_USER: tuist
      CLICKHOUSE_PASSWORD: tuist
      CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: "1"
    ports:
      - "${CLICKHOUSE_HTTP_PORT:-8124}:8123"
      - "${CLICKHOUSE_NATIVE_PORT:-9004}:9000"
    volumes:
      - ./clickhouse-config.xml:/etc/clickhouse-server/config.d/tuist.xml:ro
      - clickhouse-data:/var/lib/clickhouse
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "wget --quiet --output-document=- --user \"$$CLICKHOUSE_USER\" --password \"$$CLICKHOUSE_PASSWORD\" \"http://127.0.0.1:8123/?database=$$CLICKHOUSE_DB&query=SELECT%201\" | grep -q '^1$'",
        ]
      interval: 5s
      timeout: 5s
      retries: 30

  minio:
    image: minio/minio:RELEASE.2025-09-07T16-13-09Z
    restart: unless-stopped
    command: server /data --address ":9000" --console-address ":9003"
    environment:
      MINIO_ROOT_USER: tuist
      MINIO_ROOT_PASSWORD: tuist_dev_password
    ports:
      - "${MINIO_API_PORT:-9002}:9000"
      - "${MINIO_CONSOLE_PORT:-9003}:9003"
    volumes:
      - minio-data:/data
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:9000/minio/health/ready >/dev/null"]
      interval: 5s
      timeout: 5s
      retries: 20

  minio-init:
    image: minio/mc:RELEASE.2025-08-13T08-35-41Z
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set local http://minio:9000 tuist tuist_dev_password &&
      mc mb --ignore-existing local/tuist-artifacts &&
      mc mb --ignore-existing local/tuist-cache &&
      mc mb --ignore-existing local/tuist-xcode-cache
      "

  kura:
    image: ${KURA_IMAGE:-ghcr.io/tuist/kura:latest}
    restart: unless-stopped
    environment:
      # One listener serves the HTTP cache API and REAPI gRPC (h2c).
      KURA_PORT: "4000"
      KURA_INTERNAL_PORT: "7443"
      KURA_TENANT_ID: default
      KURA_REGION: local
      KURA_TMP_DIR: /tmp/kura
      KURA_DATA_DIR: /var/cache/kura
      KURA_NODE_URL: http://kura:7443
      KURA_PEERS: ""
      KURA_OTEL_SERVICE_NAME: kura-local
      KURA_OTEL_DEPLOYMENT_ENVIRONMENT: local
    ports:
      - "${KURA_HTTP_PORT:-4000}:4000"
    volumes:
      - kura-data:/var/cache/kura
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:4000/up >/dev/null"]
      interval: 5s
      timeout: 5s
      retries: 30

  tuist:
    image: ${TUIST_IMAGE:-ghcr.io/tuist/tuist:latest}
    restart: unless-stopped
    env_file:
      - .env
    depends_on:
      postgres:
        condition: service_healthy
      clickhouse:
        condition: service_healthy
      minio-init:
        condition: service_completed_successfully
      kura:
        condition: service_healthy
    environment:
      DATABASE_URL: postgres://tuist:tuist@postgres:5432/tuist
      TUIST_USE_SSL_FOR_DATABASE: "0"
      TUIST_CLICKHOUSE_URL: http://tuist:tuist@clickhouse:8123/tuist
      TUIST_APP_URL: http://localhost:${TUIST_PORT:-8080}
      TUIST_HOSTED: "0"
      TUIST_S3_ACCESS_KEY_ID: tuist
      TUIST_S3_SECRET_ACCESS_KEY: tuist_dev_password
      TUIST_S3_BUCKET_NAME: tuist-artifacts
      TUIST_S3_REGION: auto
      TUIST_S3_ENDPOINT: http://minio:9000
      TUIST_CACHE_S3_BUCKET_NAME: tuist-cache
      TUIST_CACHE_XCODE_S3_BUCKET_NAME: tuist-xcode-cache
      TUIST_KURA_ENDPOINTS: http://kura:4000
      TUIST_SKIP_EMAIL_CONFIRMATION: "true"
      TUIST_LOG_LEVEL: info
    ports:
      - "${TUIST_PORT:-8080}:8080"
      - "${TUIST_METRICS_PORT:-9091}:9091"
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:8080/ready >/dev/null"]
      interval: 10s
      timeout: 5s
      retries: 30

volumes:
  postgres-data:
  clickhouse-data:
  clickhouse-keeper-data:
  minio-data:
  kura-data:
