# Multi-stage Dockerfile that builds the Python server.
# Run from the service root (../). Context includes proto/, server/,
# client-python/.
FROM python:3.12-slim AS build
WORKDIR /src
RUN apt-get update && apt-get install -y --no-install-recommends \
    protobuf-compiler build-essential \
    && rm -rf /var/lib/apt/lists/*

# Install uv for fast deps.
RUN pip install --no-cache-dir uv

COPY proto         /src/proto
COPY client-python /src/client-python
COPY server        /src/server

# Codegen first (both client and server need the stubs).
RUN cd /src/server          && bash codegen.sh
RUN cd /src/client-python   && bash codegen.sh

# Build the server's venv.
WORKDIR /src/server
RUN uv sync --frozen --no-dev

FROM python:3.12-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
    libssl3 ca-certificates \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /app

COPY --from=build /src/server          /app/server
COPY --from=build /src/client-python   /app/client-python
COPY --from=build /src/proto           /app/proto

EXPOSE 50051
ENV PYTHONPATH=/app/server/src:/app/client-python/src
ENTRYPOINT ["python", "-m", "{{ service_name_snake }}_server.main"]
