FROM python:3.13-slim

WORKDIR /app

RUN apt-get update && apt-get install -y --no-install-recommends \
    curl ca-certificates \
    libglib2.0-0 libpango-1.0-0 libpangoft2-1.0-0 libharfbuzz0b libfontconfig1 \
    fonts-dejavu-core \
    && rm -rf /var/lib/apt/lists/*

# Optional: the docker CLI so the (admin-only) container manager can drive the host
# docker daemon. Off by default; the container compose override turns it on.
ARG INSTALL_DOCKER_CLI=false
RUN if [ "$INSTALL_DOCKER_CLI" = "true" ]; then \
        install -m 0755 -d /etc/apt/keyrings && \
        curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc && \
        chmod a+r /etc/apt/keyrings/docker.asc && \
        echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian bookworm stable" > /etc/apt/sources.list.d/docker.list && \
        apt-get update && apt-get install -y --no-install-recommends docker-ce-cli && \
        rm -rf /var/lib/apt/lists/* ; \
    fi

ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright

COPY pyproject.toml .
RUN mkdir -p devplacepy && touch devplacepy/__init__.py \
    && pip install --no-cache-dir ".[bots]" \
    && python -m playwright install --with-deps chromium \
    && chmod -R a+rx /ms-playwright

COPY devplacepy/ devplacepy/

RUN pip install --no-cache-dir --no-deps --force-reinstall .

EXPOSE 10500

ENV DEVPLACE_WEB_WORKERS=2
ENV DEVPLACE_TEMPLATE_AUTO_RELOAD=0

HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=120s --start-interval=2s \
    CMD curl -f http://localhost:10500/ || exit 1

CMD ["sh", "-c", "DEVPLACE_STATIC_VERSION=${DEVPLACE_STATIC_VERSION:-$(date +%s)} exec uvicorn devplacepy.main:app --host 0.0.0.0 --port 10500 --workers 2 --backlog 8192 --proxy-headers --forwarded-allow-ips '*'"]
