FROM python:3.12-slim

# Install build deps for asyncpg
RUN apt-get update && \
    apt-get install -y --no-install-recommends libpq-dev gcc && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy build files first for layer caching
COPY pyproject.toml README.md ./

# Copy source
COPY loom/ loom/

# Install with GCP extras
RUN pip install --no-cache-dir ".[gcp]"

# Non-root user
RUN useradd --create-home loom
USER loom

EXPOSE 8765

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

ENTRYPOINT ["python", "-m", "loom.mcp"]
