# Server deployment image.
#
# No browser is bundled on purpose. On this deployment the crawler attaches over
# CDP to the Chrome already running on the host (see the 接管已有 Chrome setting),
# so shipping a second copy of Chromium would only add hundreds of megabytes and
# a login state that nothing uses. The Playwright Python package is still needed
# -- that is what speaks CDP -- hence PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD.
FROM python:3.11-slim

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 \
    TZ=Asia/Shanghai

# asyncmy compiles a Cython extension, so a toolchain has to exist at build time.
# It is left installed: purging it risks taking libmysqlclient with it, and a
# slightly larger image is cheaper than a runtime that fails months later.
RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      build-essential pkg-config default-libmysqlclient-dev \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Requirements first: this layer only rebuilds when the pins actually change.
ARG PIP_INDEX=https://pypi.tuna.tsinghua.edu.cn/simple
COPY requirements.txt ./
RUN pip install --no-cache-dir -i "$PIP_INDEX" -r requirements.txt

COPY . .

# The WebUI bundle lives at api/webui, which is gitignored -- it is built on the
# workstation (`cd webui && npm run build`) and shipped inside the build context.
# Without this check a missing bundle is invisible until someone opens the page
# and gets the bare JSON stub from serve_frontend().
RUN test -f api/webui/index.html \
 || (echo "ERROR: api/webui/index.html is missing. Run 'cd webui && npm run build' before building the image." >&2; exit 1)

EXPOSE 18051

# api.main reads MC_HOST / MC_PORT from the environment; compose supplies both.
CMD ["python", "-m", "api.main"]
