FROM python:3.12-slim

WORKDIR /app

# Stage 1: Install dependencies only (cached layer).
# Build a throwaway package from minimal source so pip resolves all deps,
# then remove the incomplete package itself.
COPY pyproject.toml README.md ./
COPY mnemory/__init__.py mnemory/__init__.py
RUN pip install --no-cache-dir ".[all]" \
    && pip uninstall -y mnemory

# Stage 2: Copy full source and install the package (no deps needed).
COPY mnemory/ mnemory/
RUN pip install --no-cache-dir --no-deps .

# Data directory for vector store, artifacts, and history.
# Override ~/.mnemory default so Docker volumes mount cleanly at /data.
ENV DATA_DIR=/data
RUN mkdir -p /data

EXPOSE 8050

# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8050/health')" || exit 1

CMD ["mnemory"]
