# archie-agent image: multi-ecosystem dev environment for agent workloads.
# Pre-installs system tools agents can't install themselves (no apt, no sudo).
# Language packages are installed at runtime via uv/npm/go.
#
# Build: task image:agent   (or docker build --build-arg RUNTIME_VERSION=<ver>)
# A build that names no version stamps none, and the agent reports dev.

# ── builder ──
FROM golang:1.27-trixie AS builder
ARG RUNTIME_VERSION=dev
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build \
    -ldflags "-X github.com/samcharles93/archie-core/internal/app/agentworker.version=${RUNTIME_VERSION} -X github.com/samcharles93/archie-core/internal/installtype.buildType=container" \
    -o /usr/local/bin/archie-agent ./cmd/archie-agent/

FROM node:24-trixie-slim AS node

# ── runtime ──
FROM debian:trixie-slim
ARG RUNTIME_VERSION=dev
LABEL org.opencontainers.image.version="${RUNTIME_VERSION}"

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    git \
    gh \
    jq \
    ripgrep \
    unzip \
    xz-utils \
    build-essential \
    openssh-client \
    && rm -rf /var/lib/apt/lists/*

# ── Node.js 24 ──
COPY --from=node /usr/local/bin/node /usr/local/bin/node
COPY --from=node /usr/local/include/node /usr/local/include/node
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \
    ln -s ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx && \
    npm install -g pnpm typescript tsx esbuild stylelint htmlhint && \
    npm cache clean --force

# ── Go ──
# Reuse the builder's toolchain so the runtime Go can't drift from the one
# archie-agent was compiled with.
COPY --from=builder /usr/local/go /usr/local/go
ENV GOPATH="/go"
# /go/bin must be on PATH or anything `go install` puts there is unreachable.
ENV PATH="/usr/local/go/bin:/go/bin:${PATH}"

ARG GOLANGCI_LINT_VERSION=2.14.0
ARG GOFUMPT_VERSION=0.11.0
ARG BUF_VERSION=1.72.0
# Pinned to the versions tools/proto.sh installs; buf generate shells out to them.
ARG PROTOC_GEN_GO_VERSION=1.36.11
ARG PROTOC_GEN_GO_GRPC_VERSION=1.6.2

RUN curl -fsSL "https://github.com/golangci/golangci-lint/releases/download/v${GOLANGCI_LINT_VERSION}/golangci-lint-${GOLANGCI_LINT_VERSION}-linux-amd64.tar.gz" \
      | tar -xz -C /usr/local/bin --strip-components=1 \
        "golangci-lint-${GOLANGCI_LINT_VERSION}-linux-amd64/golangci-lint" && \
    curl -fsSL -o /usr/local/bin/gofumpt \
      "https://github.com/mvdan/gofumpt/releases/download/v${GOFUMPT_VERSION}/gofumpt_v${GOFUMPT_VERSION}_linux_amd64" && \
    curl -fsSL -o /usr/local/bin/buf \
      "https://github.com/bufbuild/buf/releases/download/v${BUF_VERSION}/buf-Linux-x86_64" && \
    chmod +x /usr/local/bin/gofumpt /usr/local/bin/buf && \
    GOBIN=/usr/local/bin go install "google.golang.org/protobuf/cmd/protoc-gen-go@v${PROTOC_GEN_GO_VERSION}" && \
    GOBIN=/usr/local/bin go install "google.golang.org/grpc/cmd/protoc-gen-go-grpc@v${PROTOC_GEN_GO_GRPC_VERSION}" && \
    go clean -cache -modcache

# ── Python via uv ─────────────────────────────────────────────────────
ARG PYTHON_VERSION=3.14
RUN curl -fsSL https://astral.sh/uv/install.sh | sh
ENV PATH="/root/.local/bin:${PATH}"
RUN uv python install --default "${PYTHON_VERSION}" && rm -rf /root/.cache/uv

# Point each toolchain at its cache volume (internal/storage). Unset, pnpm puts
# its store on the worktree's filesystem, inside the repository the agent commits.
ENV GOMODCACHE=/data/cache/go/mod \
    GOCACHE=/data/cache/go/build \
    npm_config_cache=/data/cache/node \
    npm_config_store_dir=/data/cache/pnpm \
    DENO_DIR=/data/cache/deno \
    BUN_INSTALL_CACHE_DIR=/data/cache/bun \
    PIP_CACHE_DIR=/data/cache/pip \
    UV_CACHE_DIR=/data/cache/pip \
    CARGO_HOME=/data/cache/cargo

COPY --from=builder /usr/local/bin/archie-agent /usr/local/bin/archie-agent

# The daemon injects NATS_URL and, when configured, NATS_TOKEN.
ENTRYPOINT ["archie-agent"]
