services:
  zitadel:
    user: "${ZITADEL_DEV_UID}"
    image: "${ZITADEL_IMAGE:-ghcr.io/zitadel/zitadel:latest}"
    command: 'start-from-init --masterkey "MasterkeyNeedsToHave32Characters" --tlsMode disabled --config /zitadel.yaml --steps /zitadel.yaml'
    ports:
      - "8080:8080"
    volumes:
      - ./machinekey:/machinekey
      - ./zitadel.yaml:/zitadel.yaml
    depends_on:
      db:
        condition: "service_healthy"

  db:
    restart: 'always'
    image: 'postgres:latest'
    environment:
      - POSTGRES_USER=zitadel
      - PGUSER=zitadel
      - POSTGRES_DB=zitadel
      - POSTGRES_HOST_AUTH_METHOD=trust
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c shared_buffers=1GB -c work_mem=16MB -c effective_io_concurrency=100 -c wal_level=minimal -c archive_mode=off -c max_wal_senders=0
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: '10s'
      timeout: '30s'
      retries: 5
      start_period: '20s'
    ports:
      - 5432:5432

  wait_for_zitadel:
    image: curlimages/curl:8.00.1
    command:
      [
        "/bin/sh",
        "-c",
        "i=0; while ! curl http://zitadel:8080/debug/ready && [ $$i -lt 30 ]; do sleep 1; i=$$((i+1)); done; [ $$i -eq 120 ] && exit 1 || exit 0",
      ]
    depends_on:
      - zitadel

  setup:
    user: "${ZITADEL_DEV_UID}"
    container_name: setup
    build: .
    environment:
      KEY: /key/zitadel-admin-sa.json
      SERVICE: http://zitadel:8080
      WRITE_ENVIRONMENT_FILE: /apps/login/.env.local
    volumes:
      - "./machinekey:/key"
      - "../apps/login:/apps/login"
    depends_on:
      wait_for_zitadel:
        condition: "service_completed_successfully"
