# aijianzhan full stack (default)
# Data under ./.runtime/
# Web: host-built dist mounted into nginx (no node image pull each time)
#
# 宿主机端口默认绑 127.0.0.1，避免与同机 yh_web（8088/9080/9081 + 宿主机 80/443）及公网误暴露冲突。
# 可用 .env 覆盖（compose 变量替换），例如宿主机已有 Postgres 时：
#   AIJZ_PG_PUBLISH=127.0.0.1:15432
# 需要局域网访问时：
#   AIJZ_WEB_PUBLISH=0.0.0.0:5173
#
# China: base images default to DaoCloud mirror via DOCKER_BASE_REGISTRY
#   docker compose up -d --build
#   docker compose down

x-build-args: &build_args
  BASE_REGISTRY: ${DOCKER_BASE_REGISTRY:-docker.m.daocloud.io/library}

services:
  postgres:
    image: ${DOCKER_BASE_REGISTRY:-docker.m.daocloud.io/library}/postgres:16-alpine
    environment:
      POSTGRES_USER: platform
      POSTGRES_PASSWORD: platform
      POSTGRES_DB: platform
    ports:
      - "${AIJZ_PG_PUBLISH:-127.0.0.1:5432}:5432"
    volumes:
      - ./.runtime/pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U platform -d platform"]
      interval: 3s
      timeout: 5s
      retries: 20

  platform:
    build:
      context: ./platform
      args:
        <<: *build_args
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "${AIJZ_PLATFORM_PUBLISH:-127.0.0.1:8888}:8888"
    volumes:
      - ./.runtime/uploads:/app/data/uploads
      - ./.runtime/dbsync:/app/data/dbsync
      - ./platform/etc/platform.docker.yaml:/app/etc/platform.yaml:ro
    environment:
      TZ: Asia/Shanghai

  ai:
    build:
      context: ./ai-service
      args:
        <<: *build_args
    ports:
      - "${AIJZ_AI_PUBLISH:-127.0.0.1:8001}:8001"
    env_file:
      - .env
    volumes:
      - ./ai-service:/app
      - ./ai-service/etc/llm.yaml:/app/etc/llm.yaml:ro
      - ./test:/test:ro
      - ./.runtime:/runtime
    environment:
      TZ: Asia/Shanghai
      AI_CONFIG_PATH: /app/etc/llm.yaml
      DEMO_FIXTURES_DIR: /test
      GENERATE_LOG_DIR: /runtime/logs/generate
      FIDELITY_SHOT_DIR: /runtime/fidelity_shots
      WEB_BASE: http://web:80
      PLATFORM_BASE: http://platform:8888
      GATEWAY_BASE: http://gateway:8180
      LLM_PROVIDER: ${LLM_PROVIDER:-deepseek}
      DEEPSEEK_API_KEY: ${DEEPSEEK_API_KEY:-}
      MINIMAX_API_KEY: ${MINIMAX_API_KEY:-}
      PIP_INDEX_URL: https://pypi.tuna.tsinghua.edu.cn/simple
      PIP_TRUSTED_HOST: pypi.tuna.tsinghua.edu.cn

  gateway:
    build:
      context: ./gateway
      args:
        <<: *build_args
    depends_on:
      - platform
      - ai
    ports:
      - "${AIJZ_GATEWAY_PUBLISH:-127.0.0.1:8180}:8180"
    volumes:
      - ./gateway/etc/gateway.docker.yaml:/app/etc/gateway.yaml:ro
    environment:
      TZ: Asia/Shanghai
      GOPROXY: https://goproxy.cn,direct

  web:
    image: ${DOCKER_BASE_REGISTRY:-docker.m.daocloud.io/library}/nginx:1.27-alpine
    depends_on:
      - gateway
    ports:
      - "${AIJZ_WEB_PUBLISH:-127.0.0.1:5173}:80"
    volumes:
      - ./web/dist:/usr/share/nginx/html:ro
      - ./web/nginx.conf:/etc/nginx/conf.d/default.conf:ro
    environment:
      TZ: Asia/Shanghai
