# CleverAgents Server — production container image (ADR-048)
#
# Multi-stage build:
#   1. Builder stage — installs dependencies into a venv
#   2. Runtime stage — copies the venv and source for minimal image size
#
# Usage:
#   docker build -f docker/Dockerfile -t cleveragents-server .
#   docker run -p 8000:8000 cleveragents-server

# ---------------------------------------------------------------------------
# Builder
# ---------------------------------------------------------------------------
FROM python:3.13-slim AS builder

WORKDIR /build

# Install build dependencies
RUN pip install --no-cache-dir hatchling>=1.21.0

# Copy dependency metadata first for layer caching
COPY pyproject.toml README.md ./
COPY src/ src/

# Build wheel
RUN pip install --no-cache-dir --prefix=/install .

# ---------------------------------------------------------------------------
# Runtime
# ---------------------------------------------------------------------------
FROM python:3.13-slim AS runtime

# Security: run as non-root
RUN groupadd --gid 1000 cleveragents \
    && useradd --uid 1000 --gid cleveragents --shell /bin/bash --create-home cleveragents

WORKDIR /app

# Copy installed packages from builder
COPY --from=builder /install /usr/local

# Copy source (for editable installs / debugging)
COPY src/ src/

# Create data directory
RUN mkdir -p /app/.cleveragents && chown -R cleveragents:cleveragents /app

USER cleveragents

# Server port
EXPOSE 8000

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

# Default command — run the server
CMD ["uvicorn", "cleveragents.server.app:app", "--host", "0.0.0.0", "--port", "8000"]
