# ── Stage 1: Dependencies ────────────────────────────────────────────────────
FROM python:3.12-slim AS builder

WORKDIR /build

# Install system dependencies and build tools
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    && rm -rf /var/lib/apt/lists/* \
    && python -m pip install --no-cache-dir --upgrade pip hatchling

COPY pyproject.toml ./
RUN python -m pip install --no-cache-dir --prefix=/install .

# ── Stage 2: Runtime ─────────────────────────────────────────────────────────
FROM python:3.12-slim AS runtime

RUN groupadd --system --gid 1001 diffmem \
    && useradd  --system --uid 1001 --gid diffmem --no-create-home diffmem

WORKDIR /app

COPY --from=builder /install /usr/local

COPY diffmem_mcp/ ./diffmem_mcp/

USER diffmem

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    MCP_PORT=8003

EXPOSE 8003

HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8003/health')"

# Streamable HTTP + legacy SSE transports for remote clients (production
# standalone deployment). For every real environment, the MCP server is
# mounted as a sub-app in regional-plane (ADR-030/ADR-039).
CMD ["python", "-m", "diffmem_mcp.server", "--sse", "--host", "0.0.0.0", "--port", "8003"]
