# Multi-stage build: keep the runtime image small and cache-friendly.
# - stage `builder`: compile-deps / build the wheel if you want to vendor
#   the package without pulling from PyPI at runtime.
# - stage `runtime`: minimal CPython + uv + the published `smart-agent-mcp`.

# ---- builder (only used if you build with `--target builder`) ----
FROM python:3.13-slim AS builder
RUN pip install --no-cache-dir uv
WORKDIR /build
COPY pyproject.toml uv.lock LICENSE README.md ./
COPY smart_agent ./smart_agent
RUN uv build --wheel

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

# uv is the modern, fast Python package manager — same one we already use
# locally. Caches wheels in /tmp/uv-cache to keep container size sane.
ENV UV_LINK_MODE=copy \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    SMART_AGENT_HOME=/var/lib/smart-agent
RUN pip install --no-cache-dir uv==0.4.30

# Operational layering — `nonroot` keeps the SQLite store + AGENTS.md
# outside root-owned paths, matching the documented `$LOCALAPPDATA` /
# `~/.local/share` patterns.
RUN groupadd -r smartagent && useradd -r -g smartagent -d /home/smartagent smartagent \
    && mkdir -p /var/lib/smart-agent /home/smartagent \
    && chown -R smartagent:smartagent /var/lib/smart-agent /home/smartagent
USER smartagent:smartagent
WORKDIR /home/smartagent

# We do NOT bake a particular wheel into the image on purpose — the entry
# point uses `uvx` so the published PyPI version is the source of truth.
# This image is meant to be:
#
#   uvx --from smart-agent-mcp==<version> smart-agent serve
#
# If you prefer an image with a fixed wheel (e.g. for air-gapped deploys),
# override CMD with the wheel from the `builder` stage.

EXPOSE 0  # stdio MCP — no TCP port. The EXPOSE above is a no-op placeholder.

# stdio MCP servers can't be health-checked via TCP. If you front this
# with a process supervisor (systemd, supervisord, k8s liveness probe),
# write the probe to read `/var/lib/smart-agent/health.json` — see the
# `health_check` MCP tool + `DEPLOYMENT.md` for the suggested layout.

ENTRYPOINT ["uvx", "--from", "smart-agent-mcp", "smart-agent"]
CMD ["serve"]
