# Matrx Files — standalone microservice image.
#
# PYPI mode installs ENTIRELY from PyPI (all matrx-* deps are published; a bare
# `uv pip install matrx-files[standalone]` is proven by the independence
# gate + a PyPI-only boot test). Build from anywhere:
#   docker build -t matrx-files packages/matrx-files
# Pin the version at build time for reproducible deploys:
#   docker build --build-arg MATRX_FILES_VERSION==0.1.0 -t matrx-files:0.1.0 packages/matrx-files
FROM python:3.14-slim

# System binaries:
#   tesseract-ocr           — pytesseract OCR backend (same binary the CI gate installs).
#   libreoffice-{writer,calc,impress} + -core — the Office codec's LibreOffice lane:
#       reads legacy .doc/.ppt/.xls (→ OpenXML) and renders any Office doc → PDF
#       for page-1 thumbnails. --no-install-recommends keeps the three apps
#       (~450 MB) instead of the full ~1 GB suite; the codec degrades to
#       pure-python (OpenXML only) if these are ever absent.
RUN apt-get update -qq && apt-get install -y -qq --no-install-recommends \
    tesseract-ocr \
    libreoffice-core \
    libreoffice-writer \
    libreoffice-calc \
    libreoffice-impress \
    && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir uv

# TWO install modes, one Dockerfile:
#   SOURCE (how the Server Manager deploys, from the aidream release commit):
#     the build context holds requirements.txt + packages/<matrx-files and every
#     matrx-* sibling it installs>, written by the Manager's source bundle
#     (infra/matrx-ship/server-manager/src/microservice_source.js).
#   PYPI (manual / independence proof): context is packages/matrx-files; no
#     requirements.txt, so the published package is installed.
ARG MATRX_FILES_VERSION=""
COPY . /build/
RUN if [ -f /build/requirements.txt ]; then \
      uv pip install --system --no-sources -r /build/requirements.txt; \
    else \
      uv pip install --system "matrx-files[standalone]${MATRX_FILES_VERSION}"; \
    fi && rm -rf /build

# What runs, readable from /health and the image labels (empty in PYPI mode).
ARG MATRX_BUILD_COMMIT=""
ARG MATRX_SOURCE_ID=""
ENV MATRX_BUILD_COMMIT=${MATRX_BUILD_COMMIT} MATRX_SOURCE_ID=${MATRX_SOURCE_ID}
LABEL org.opencontainers.image.revision=${MATRX_BUILD_COMMIT} matrx.source-id=${MATRX_SOURCE_ID}
RUN printf '%s\n%s\n' "${MATRX_BUILD_COMMIT}" "${MATRX_SOURCE_ID}" > /etc/matrx-build-identity

ENV MATRX_FILES_BASE_DIR=/data/matrx-files
EXPOSE 8080
CMD ["uvicorn", "matrx_files.standalone.app:create_app", "--factory", "--host", "0.0.0.0", "--port", "8080"]
