FROM python:3.11-slim AS base

WORKDIR /app
RUN useradd -u 1000 -m appuser
USER appuser

ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app
ENV UV_PROJECT_ENVIRONMENT=/usr/local

# Install uv and git for reproducible platform SDK installation.
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates git \
    && rm -rf /var/lib/apt/lists/* \
    && pip install --no-cache-dir uv

# Copy dependency manifests first for layer caching.
COPY pyproject.toml uv.lock* ./

# Install project dependencies into the system environment.
RUN uv sync --no-dev --no-install-project --frozen

FROM base AS development

COPY . .
RUN uv sync --no-dev --frozen

# Local fullstack/dev builds can override the SDK with a checked-out agent_api
# build context, matching platform-agent's development Dockerfile pattern.
COPY --from=agent_api . /agent_api/
RUN python -m pip install --no-cache-dir --ignore-requires-python -e /agent_api/

CMD ["python", "-m", "adapter.matrix.bot"]

FROM base AS production

COPY . .
RUN uv sync --no-dev --frozen

# Production builds follow the platform-agent pattern: install the API SDK from
# the platform Git repository instead of relying on local external/ clones.
ARG LAMBDA_AGENT_API_REF=master
RUN python -m pip install --no-cache-dir --ignore-requires-python \
    "git+https://git.lambda.coredump.ru/platform/agent_api.git@${LAMBDA_AGENT_API_REF}"

CMD ["python", "-m", "adapter.matrix.bot"]
