FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

WORKDIR /app

# Install server dependencies first for better layer caching
COPY sasi_server/requirements.txt /app/sasi_server/requirements.txt
RUN python -m pip install --no-cache-dir -U pip \
    && python -m pip install --no-cache-dir -r /app/sasi_server/requirements.txt

# Install the local SDK package (so sasi_server can import sasi_sdk)
COPY pyproject.toml /app/pyproject.toml
COPY README.md /app/README.md
COPY sasi_sdk /app/sasi_sdk
RUN python -m pip install --no-cache-dir -e /app

# Bundle MiniLM ONNX into the image (crisis semantic anchors). Do not set
# SASI_DISABLE_SEMANTIC or SASI_SERVERLESS_MODE in production if you need this path.
# Override path at runtime with SASI_ONNX_MODEL_PATH if the file is mounted elsewhere.
RUN apt-get update \
    && apt-get install -y --no-install-recommends curl ca-certificates \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /app/sasi_sdk/models/minilm_onnx \
    && curl -fsSL -o /app/sasi_sdk/models/minilm_onnx/all-MiniLM-L6-v2.onnx \
        "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx"

# Copy the server code
COPY sasi_server /app/sasi_server

EXPOSE 8080

CMD ["python", "-m", "uvicorn", "sasi_server.main:app", "--host", "0.0.0.0", "--port", "8080"]
