# Build arguments (must be before first FROM for cross-stage visibility)
ARG NODE_VERSION=24
ARG PNPM_VERSION=12.6.0
ARG GO_VERSION=1.26.10

# Stage 1: Build frontend assets (for playground UI)
FROM node:${NODE_VERSION}-bookworm-slim AS ui-builder
ARG PNPM_VERSION
RUN npm install -g pnpm@${PNPM_VERSION}
WORKDIR /ui
COPY ui/package.json ui/pnpm-lock.yaml ui/pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile
COPY ui/ ./
RUN pnpm run build

# Stage 2: Go sandbox with pre-built frontend
FROM golang:${GO_VERSION}-bookworm

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        bash git ca-certificates curl jq sudo procps \
    && curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
    && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
    && apt-get update && apt-get install -y gh \
    && rm -rf /var/lib/apt/lists/*

# Install Go development tools
# air pinned: v1.67.2+ requires go >= 1.26, image runs GO_VERSION with GOTOOLCHAIN=local
RUN go install github.com/air-verse/air@v1.67.1 \
    && go install github.com/go-delve/delve/cmd/dlv@latest

# Copy node runtime from ui-builder stage (for devcontainer / interactive use)
COPY --from=ui-builder /usr/local/bin/node /usr/local/bin/
COPY --from=ui-builder /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -sf ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
    && ln -sf ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx \
    && ln -sf ../lib/node_modules/corepack/dist/corepack.js /usr/local/bin/corepack
ARG PNPM_VERSION
RUN npm install -g pnpm@${PNPM_VERSION}

# Copy pre-built frontend for playground UI cache
COPY --from=ui-builder /ui/dist /ui-dist

COPY docker/sandbox/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

WORKDIR /workspace

COPY go.mod go.sum ./
# The module proxy sometimes drops a download mid-stream; retry before failing the build.
RUN for i in 1 2 3; do go mod download && exit 0; echo "go mod download failed (attempt $i), retrying" >&2; sleep 10; done; exit 1
RUN mkdir -p /go/build-cache /go/pkg/mod && chmod -R 0777 /go/build-cache /go/pkg/mod

ENTRYPOINT ["/entrypoint.sh"]
CMD ["bash", "-c", "./scripts/test.sh"]
