# 本機開發 / k6 測試依賴：MongoDB、Redis、MailHog、Postgres、OpenLDAP、ZITADEL
#
# 啟動：
#   make deps-up          → mongo + redis（最小，吃 etc/gateway.dev.yaml）
#   make deps-up-smtp     → + mailhog（profile smtp）
#   make k6-up            → mongo + redis + mailhog + postgres + zitadel（吃 etc/gateway.k6.yaml）
#
# ZITADEL admin PAT 會寫到 deploy/zitadel/machinekey/zitadel-admin-sa.token

services:
  mongo:
    image: mongo:7
    container_name: gateway-mongo
    restart: unless-stopped
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_DATABASE: gateway
    volumes:
      - mongo_data:/data/db
      - ./mongo/init:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping').ok"]
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 10s

  redis:
    image: redis:7-alpine
    container_name: gateway-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

  mailhog:
    profiles: ["smtp", "k6"]
    image: mailhog/mailhog:v1.0.1
    container_name: gateway-mailhog
    restart: unless-stopped
    ports:
      - "1025:1025"   # SMTP
      - "8025:8025"   # Web UI / HTTP API

  # ===== ZITADEL stack（profile k6）=====
  postgres:
    profiles: ["k6"]
    image: postgres:17-alpine
    container_name: gateway-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: zitadel
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres", "-d", "zitadel"]
      interval: 5s
      timeout: 3s
      retries: 20

  openldap:
    profiles: ["k6"]
    image: osixia/openldap:1.5.0
    container_name: gateway-openldap
    restart: unless-stopped
    environment:
      LDAP_ORGANISATION: "GatewayDev"
      LDAP_DOMAIN: "gateway.local"
      LDAP_ADMIN_PASSWORD: "admin"
      LDAP_CONFIG_PASSWORD: "config"
      LDAP_TLS: "false"
    ports:
      - "389:389"
    volumes:
      - openldap_data:/var/lib/ldap
      - openldap_config:/etc/ldap/slapd.d
    healthcheck:
      test:
        [
          "CMD",
          "ldapsearch",
          "-x",
          "-H",
          "ldap://localhost",
          "-b",
          "dc=gateway,dc=local",
          "-D",
          "cn=admin,dc=gateway,dc=local",
          "-w",
          "admin",
          "-LLL",
          "-s",
          "base",
          "(objectClass=*)",
          "dn",
        ]
      interval: 5s
      timeout: 5s
      retries: 24
      start_period: 20s

  zitadel:
    profiles: ["k6"]
    image: ghcr.io/zitadel/zitadel:v2.65.0
    container_name: gateway-zitadel
    restart: unless-stopped
    command: ["start-from-init", "--masterkey", "MasterkeyNeedsToHave32Characters", "--tlsMode", "disabled", "--config", "/etc/zitadel/zitadel.yaml", "--steps", "/etc/zitadel/steps.yaml"]
    depends_on:
      postgres:
        condition: service_healthy
      openldap:
        condition: service_healthy
    ports:
      - "8080:8080"
    volumes:
      - ./zitadel/zitadel.yaml:/etc/zitadel/zitadel.yaml:ro
      - ./zitadel/steps.yaml:/etc/zitadel/steps.yaml:ro
      - ./zitadel/machinekey:/machinekey
    healthcheck:
      # zitadel image lacks wget/curl; use /proc/net/tcp to verify the port
      # is in LISTEN state (10 = LISTEN). Cheap, no binary deps.
      test: ["CMD-SHELL", "grep -q ':1F90 .* 0A' /proc/net/tcp || exit 1"]
      interval: 5s
      timeout: 3s
      retries: 60
      start_period: 60s

volumes:
  mongo_data:
  redis_data:
  postgres_data:
  openldap_data:
  openldap_config:
