name: haixun-prod

services:
  mongo:
    image: mongo:7
    restart: unless-stopped
    environment:
      MONGO_INITDB_DATABASE: haixun
    # named volume：重啟/改版不會清資料（只有 prod-wipe-data 或 docker volume rm 才會）
    volumes:
      - mongo_data:/data/db
    healthcheck:
      test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping').ok"]
      interval: 5s
      timeout: 5s
      retries: 12
      start_period: 15s

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    # AOF + named volume：重啟後 queue/lock 狀態可從磁碟恢復
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 12

  api:
    build:
      context: ..
      dockerfile: deploy/Dockerfile.api
    restart: unless-stopped
    env_file:
      - .env
    depends_on:
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:8890/api/v1/health >/dev/null || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 12
      start_period: 20s

  go-worker:
    build:
      context: ..
      dockerfile: deploy/Dockerfile.api
    restart: unless-stopped
    entrypoint: ["/app/deploy/docker/entrypoint-worker.sh"]
    env_file:
      - .env
    depends_on:
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
      api:
        condition: service_healthy

  node-worker-style-8d:
    build:
      context: ..
      dockerfile: deploy/Dockerfile.node-worker
    restart: unless-stopped
    env_file:
      - .env
    environment:
      HAIXUN_BACKEND_URL: http://api:8890
      HAIXUN_WORKER_SECRET: ${HAIXUN_WORKER_SECRET}
      HAIXUN_NODE_WORKER_ID: ${HAIXUN_NODE_WORKER_ID:-}
      HAIXUN_WORKER_POLL_MS: ${HAIXUN_WORKER_POLL_MS:-3000}
    depends_on:
      api:
        condition: service_healthy

  web:
    build:
      context: ..
      dockerfile: deploy/Dockerfile.web.static
    restart: unless-stopped
    ports:
      - "${HAIXUN_WEB_PORT:-8080}:80"
    depends_on:
      api:
        condition: service_healthy

  init:
    profiles: ["init"]
    build:
      context: ..
      dockerfile: deploy/Dockerfile.api
    entrypoint: ["/app/deploy/docker/entrypoint-init.sh"]
    env_file:
      - .env
    depends_on:
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy

volumes:
  mongo_data:
  redis_data: