# Quick start with the published Docker Hub runtime image.
#
#   docker compose -f docker-compose.release.yml up -d --wait
#
# Requires this repository checkout (gateway TLS + nginx conf are bind-mounted).
# Seed runs once after the simulator is healthy.
#
# Override the image tag:
#   IMAGE_TAG=0.1.0 docker compose -f docker-compose.release.yml up -d --wait
#
# Clients should hit api-gateway HTTPS (:443).

name: vmware-api-simulator-release

x-app-image: &app-image
  image: ${DOCKER_IMAGE:-inecs/vmware-api-simulator}:${IMAGE_TAG:-latest}

x-app-env: &app-env
  DATABASE_URL: postgresql://vmware:vmware@postgres:5432/vmware_simulator
  ENABLE_PVE_STUB: "false"
  LOG_LEVEL: ${LOG_LEVEL:-INFO}
  TICKET_SIGNING_KEY: ${TICKET_SIGNING_KEY:-development-only-signing-key-change-me}
  TASK_WORKER_CONCURRENCY: ${TASK_WORKER_CONCURRENCY:-2}
  SIMULATION_TIME_SCALE: ${SIMULATION_TIME_SCALE:-10}
  APP_PORT: "8080"

networks:
  simulator:
    driver: bridge

volumes:
  postgres-data:

services:
  postgres:
    image: postgres:17.5-bookworm
    restart: unless-stopped
    networks: [simulator]
    environment:
      POSTGRES_DB: vmware_simulator
      POSTGRES_USER: vmware
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-vmware}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U vmware -d vmware_simulator"]
      interval: 5s
      timeout: 3s
      retries: 10
      start_period: 5s
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - "${POSTGRES_PORT:-127.0.0.1:5434}:5432"

  migrate:
    <<: *app-image
    networks: [simulator]
    environment:
      <<: *app-env
      DATABASE_URL: postgresql://vmware:${POSTGRES_PASSWORD:-vmware}@postgres:5432/vmware_simulator
    depends_on:
      postgres:
        condition: service_healthy
    entrypoint: ["python"]
    command: ["-m", "app.db.migrate_cli"]
    restart: "no"

  simulator:
    <<: *app-image
    restart: unless-stopped
    networks: [simulator]
    environment:
      <<: *app-env
      DATABASE_URL: postgresql://vmware:${POSTGRES_PASSWORD:-vmware}@postgres:5432/vmware_simulator
    depends_on:
      migrate:
        condition: service_completed_successfully
    healthcheck:
      test:
        [
          "CMD",
          "python",
          "-c",
          "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)",
        ]
      interval: 10s
      timeout: 3s
      retries: 8
      start_period: 20s
    expose:
      - "8080"

  seed:
    <<: *app-image
    networks: [simulator]
    environment:
      <<: *app-env
      DATABASE_URL: postgresql://vmware:${POSTGRES_PASSWORD:-vmware}@postgres:5432/vmware_simulator
      SEED_PROFILE: ${SEED_PROFILE:-small}
      SEED_VSPHERE_PROFILE: ${SEED_VSPHERE_PROFILE:-small}
    depends_on:
      simulator:
        condition: service_healthy
    entrypoint: ["python"]
    command: ["-m", "app.simulation.seed_cli"]
    restart: "no"

  api-gateway:
    image: nginx:1.28.0-alpine
    restart: unless-stopped
    networks: [simulator]
    depends_on:
      simulator:
        condition: service_healthy
      seed:
        condition: service_completed_successfully
    ports:
      - "${HTTP_PORT:-80}:80"
      - "${HTTPS_PORT:-443}:443"
    volumes:
      - ./docker/gateway/vmware-ports.conf:/etc/nginx/conf.d/default.conf:ro
      - ./docker/tls/server.crt:/etc/nginx/tls/server.crt:ro
      - ./docker/tls/server.key:/etc/nginx/tls/server.key:ro
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "wget -qO- http://127.0.0.1/health/live || exit 1",
        ]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 5s
