# Agents Service - Kubernetes optimized build
# Lightweight Pydantic AI agents optimized for K8s
# Optimizations:
# - Non-root user for security
# - Proper signal propagation for graceful shutdown
# - No HEALTHCHECK (K8s uses liveness/readiness probes)
# - Cache cleanup for smaller image size

FROM python:3.12-slim

WORKDIR /app

# Install uv
RUN pip install --no-cache-dir uv && \
    rm -rf ~/.cache/pip

# Copy pyproject.toml for dependency installation
COPY pyproject.toml .

# Install only agents dependencies using uv
RUN uv pip install --system --group agents && \
    rm -rf ~/.cache/uv ~/.cache/pip

# Create non-root user
RUN groupadd -r appuser -g 1001 && \
    useradd -r -g appuser -u 1001 appuser && \
    chown -R appuser:appuser /app

# Copy agents code - no dependencies on server code
# Agents use MCP tools for all operations
COPY --chown=appuser:appuser src/agents/ src/agents/
COPY --chown=appuser:appuser src/__init__.py src/

# Set environment variables
ENV PYTHONPATH="/app:$PYTHONPATH"
ENV PYTHONUNBUFFERED=1

# Expose Agents port
ARG ARCHON_AGENTS_PORT=8052
ENV ARCHON_AGENTS_PORT=${ARCHON_AGENTS_PORT}
EXPOSE ${ARCHON_AGENTS_PORT}

# Switch to non-root user
USER appuser

# Run the Agents service with proper signal propagation for K8s
# Using exec to ensure SIGTERM reaches the Python process for graceful shutdown
CMD ["sh", "-c", "exec python -m uvicorn src.agents.server:app --host 0.0.0.0 --port ${ARCHON_AGENTS_PORT}"]
