# Generated by https://smithery.ai. See: https://smithery.ai/docs/config#dockerfile
FROM python:3.12-slim

# Set working directory
WORKDIR /app

# Select dependency flavor at build time.
# Valid values: "" (core), "all", "semantic", "pdf", "scite".
ARG INSTALL_EXTRAS=""

# Install the dependencies first, from pyproject.toml alone, so this layer is
# reused until pyproject.toml changes; copying the source tree first rebuilt
# it on every commit.
#
# The semantic extra pulls sentence-transformers -> torch, and torch's default PyPI wheel
# depends on the CUDA stack (nvidia-cudnn ~445 MB, nvidia-cusparselt ~221 MB, triton ~185 MB).
# None of it is usable here: python:3.12-slim ships no CUDA runtime and the container is not
# run with --gpus. It is also what breaks the build — downloading ~850 MB of wheels through
# QEMU-emulated linux/arm64 truncates, and pip then fails its hash check.
#
# So install the CPU-only torch build from PyTorch's index first; the extras resolve against
# it afterwards. Retries/timeout are belt-and-braces for the remaining large downloads.
ARG PIP_OPTS="--no-cache-dir --retries 5 --timeout 120"
COPY pyproject.toml docker/requirements.py /tmp/deps/
RUN case ",${INSTALL_EXTRAS}," in \
         *,all,*|*,semantic,*) \
           pip install $PIP_OPTS \
             --index-url https://download.pytorch.org/whl/cpu \
             --extra-index-url https://pypi.org/simple \
             torch ;; \
       esac \
    && python /tmp/deps/requirements.py /tmp/deps/pyproject.toml "${INSTALL_EXTRAS}" > /tmp/deps/requirements.txt \
    && pip install $PIP_OPTS -r /tmp/deps/requirements.txt

# Copy the project files (see .dockerignore) and install the package itself.
# Its dependencies are already there; naming the extras again keeps this
# correct should the list above ever miss one.
COPY . /app
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN if [ -n "$INSTALL_EXTRAS" ]; then pip install $PIP_OPTS ".[${INSTALL_EXTRAS}]"; else pip install $PIP_OPTS .; fi \
    && chmod 755 /usr/local/bin/entrypoint.sh

# Run as a non-root user (defense-in-depth for multi-tenant hosts)
RUN useradd --create-home --shell /usr/sbin/nologin app \
    && chown -R app:app /app
USER app

# Default to MCP server mode; can switch to CLI via ZOTERO_APP=cli.
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
