# ============================================
# Armature — Builder Image
# ============================================
# Pre-caches Go modules and Node dependencies
# for faster custom builds. Use as a base in
# your own Dockerfile to skip dependency
# download on every build.
#
# Usage:
#   FROM ghcr.io/armature/builder:latest AS go-builder
#   COPY server/ /app/
#   RUN cd /app && go build -o /bin/armature .
#
# Or build this image locally:
#   docker build -f Dockerfile.builder -t armature-builder .
# ============================================

# ── Go module cache ─────────────────────────
FROM golang:1.23-bookworm AS go-cache
WORKDIR /cache
COPY server/go.mod server/go.sum* ./
RUN go mod download && go mod verify

# ── Node dependency cache ───────────────────
FROM node:20-alpine AS node-cache
WORKDIR /cache

# Editor bundle dependencies
COPY src/editor/package*.json ./editor/
RUN cd editor && npm ci --loglevel=warn

# Vendor libs (same versions as production Dockerfile)
RUN npm pack marked@16.3.0 && \
    npm pack dompurify@3.2.4 && \
    npm pack mermaid@11.4.1 && \
    npm pack katex@0.16.11 && \
    mkdir -p /cache/vendor-tarballs && \
    mv *.tgz /cache/vendor-tarballs/

# ── Final builder image ────────────────────
FROM golang:1.23-bookworm

# Pre-install build tools
RUN apt-get update && apt-get install -y --no-install-recommends \
    zip \
    && rm -rf /var/lib/apt/lists/*

# Go module cache (populated)
COPY --from=go-cache /go/pkg/mod /go/pkg/mod

# Node dependencies (for editor bundle builds)
COPY --from=node-cache /cache/editor/node_modules /cache/editor/node_modules

# Vendor lib tarballs (avoid re-download)
COPY --from=node-cache /cache/vendor-tarballs /cache/vendor-tarballs

# Node.js for frontend builds
COPY --from=node-cache /usr/local/bin/node /usr/local/bin/node
COPY --from=node-cache /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -sf /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm

WORKDIR /build

LABEL org.opencontainers.image.title="Armature Builder"
LABEL org.opencontainers.image.description="Pre-cached build dependencies for faster custom Armature builds"
