# syntax=docker/dockerfile:1

# ── base: Python and the runtime libraries VTK needs (shared by test and runtime) ──
FROM python:3.14-slim-trixie AS base

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PORT=3000 \
    # The container is only reachable through the reverse proxy, so trust its X-Forwarded-For
    # (needed for per-client rate limiting).
    FORWARDED_ALLOW_IPS="*"

WORKDIR /app

# Runtime libraries required by VTK.
RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender1 \
    libosmesa6 \
    && rm -rf /var/lib/apt/lists/*

# ── test: `docker build --target test .` runs the pytest suite (used by CI) ──
FROM base AS test
COPY requirements.txt requirements-dev.txt pytest.ini ./
# --prefer-binary: never compile from source (required on ARM64 where some wheels are missing)
RUN pip install --prefer-binary -r requirements-dev.txt
COPY main.py index.html google21a790a2c23c0ec5.html robots.txt sitemap.xml ./
COPY src ./src
COPY public ./public
COPY tests ./tests
RUN python -m pytest -q

# ── runtime: the image that ships ──
FROM base AS runtime

ARG VERSION=dev
ARG REVISION=unknown
LABEL org.opencontainers.image.title="InBraille" \
      org.opencontainers.image.description="Text to Braille converter with STL export for 3D printing" \
      org.opencontainers.image.source="https://git.lucasrguerra.dev.br/lucasrguerra/inbraille" \
      org.opencontainers.image.licenses="MIT" \
      org.opencontainers.image.version="${VERSION}" \
      org.opencontainers.image.revision="${REVISION}"

# curl: the Coolify healthcheck runs `curl` inside the container (the image HEALTHCHECK uses Python).
RUN apt-get update && apt-get install -y --no-install-recommends curl \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
# pip/setuptools (and the libraries vendored inside pip) are build tools: remove them from the runtime image.
RUN pip install --prefer-binary -r requirements.txt \
    && pip uninstall -y pip setuptools wheel

COPY main.py index.html google21a790a2c23c0ec5.html robots.txt sitemap.xml ./
COPY src ./src
COPY public ./public

RUN useradd --system --uid 10001 --no-create-home --shell /usr/sbin/nologin inbraille
USER 10001

EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=10s --start-period=20s --retries=3 \
    CMD ["python", "-c", "import os,urllib.request as u; u.urlopen('http://127.0.0.1:%s/' % os.environ.get('PORT','3000'), timeout=5)"]

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