# o2o-web4ai 백엔드 이미지 — 세 프로세스가 **같은 이미지**를 공유하고 command 로만 갈린다.
#   사장님 API : python web_main.py     (:9800)
#   내부 API   : python main.py         (:9801, working_dir=/app/admin/backend)
#   워커       : python worker_main.py  (수집·비전분석·빌드 잡, 포트 없음)
#
# ★ 빌드 컨텍스트가 **레포 루트**다(./solution/backend 가 아니다).
#   내부 API 진입점이 admin/backend 에 있고 도메인 코드는 solution/backend 에 있어서,
#   한 이미지에 둘 다 들어와야 한다. 나누면 requirements 를 두 번 설치하게 된다.
#
# 시크릿은 이미지에 굽지 않는다 — config 는 example(플레이스홀더)로 대체되고,
# 실제 값은 compose 의 env 로 주입된다(server_configs override).
FROM python:3.12-slim

WORKDIR /app

# 의존성 먼저 설치 (레이어 캐시 활용)
COPY solution/backend/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY solution/backend ./solution/backend
COPY admin/backend    ./admin/backend

# 시크릿 든 config.local.toml 은 .dockerignore 로 제외됨 → example(플레이스홀더)로 대체.
RUN cp solution/backend/config/config.local.toml.example solution/backend/config/config.local.toml

ENV APP_ENV=local
# ★ 내부 API(admin/backend)가 common·router·services 를 그대로 import 하는 경로.
#   도메인 코드를 복제하지 않는 대신 이 한 줄이 두 폴더를 잇는다.
ENV PYTHONPATH=/app/solution/backend

# 기본은 사장님 API. 내부 API·워커는 compose 가 working_dir·command 를 갈아끼운다.
WORKDIR /app/solution/backend

EXPOSE 9800

# 사장님 API 컨테이너용. 워커는 포트가 없어 이 체크를 끄고, 내부 API 는 :9801 로 다시 건다
# (둘 다 compose 에서 override).
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:9800/healthz', timeout=4).status==200 else 1)"

CMD ["python", "web_main.py"]
