# STEP Parser — Phase 0 CLI container
#
# Goal: prove the heavy native stack runs on linux/amd64 (Unraid target):
#   - CAD kernel        build123d → cadquery-ocp (OpenCASCADE)
#   - offscreen render  pyrender via OSMesa (software GL, no GPU needed)
#   - diagram export    cairosvg (cairo / pango)
#
# Build for Unraid (x86_64) even from an arm64 Mac (uses QEMU emulation):
#   docker buildx build --platform linux/amd64 -t step-parser:dev --load .
# or just: ./build.sh  (build.sh passes --platform linux/amd64)
FROM python:3.11-slim-bookworm

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    # Force pyrender/PyOpenGL onto the software rasterizer — no GPU on Unraid.
    PYOPENGL_PLATFORM=osmesa

# System libraries grouped by which dependency needs them:
#   OpenCASCADE / OCP runtime  : libgl1 libglu1-mesa X11 libs fontconfig libgomp1
#   pyrender offscreen (OSMesa): libosmesa6
#   cairosvg                   : libcairo2 libpango* libgdk-pixbuf libffi8
RUN apt-get update && apt-get install -y --no-install-recommends \
      libgl1 \
      libglu1-mesa \
      libosmesa6 \
      libxrender1 \
      libxext6 \
      libsm6 \
      libx11-6 \
      libxi6 \
      libfontconfig1 \
      libglib2.0-0 \
      libgomp1 \
      libcairo2 \
      libpango-1.0-0 \
      libpangocairo-1.0-0 \
      libgdk-pixbuf-2.0-0 \
      libffi8 \
      ca-certificates \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY requirements.txt .
# pyrender hard-pins PyOpenGL==3.1.0, but the OSMesa offscreen backend needs
# OSMesaCreateContextAttribs, which only exists in PyOpenGL >=3.1.5. Install
# pyrender's pin first, then force-upgrade PyOpenGL (--no-deps leaves pyrender
# itself untouched, avoiding the resolver conflict).
RUN pip install --upgrade pip \
 && pip install -r requirements.txt \
 && pip install --no-deps --upgrade "PyOpenGL==3.1.7"

# The skill source lives in skill.src/ in the repo root; copy it into the image.
# step_processor.py adds its own dir to sys.path[0], so `import modules` resolves
# regardless of the working directory.
COPY skill.src ./skill.src

# Generated artifacts (xlsx, png, svg, _EN.step) are written next to the input
# STEP file. Mount a host directory here and process files inside it.
VOLUME ["/data"]

ENTRYPOINT ["python", "skill.src/step_processor.py"]
CMD ["--help"]
