# ── Stage 1: Build frontend ───────────────────────────────────────────────────
FROM node:22-alpine AS frontend-builder
WORKDIR /app/frontend
RUN npm install -g pnpm@10.33.4
COPY frontend/package.json frontend/pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile
COPY frontend/ ./
RUN NODE_OPTIONS=--max-old-space-size=2048 pnpm build

# ── Stage 2: Build Go binary ──────────────────────────────────────────────────
FROM golang:1.26-alpine AS go-builder
RUN apk add --no-cache git
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
COPY --from=frontend-builder /app/frontend/dist ./web/dist
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /forgebucket ./cmd/forgebucket

# ── Stage 3: Minimal runtime ──────────────────────────────────────────────────
FROM alpine:3.21
RUN apk add --no-cache git ca-certificates tzdata
WORKDIR /app
COPY --from=go-builder /forgebucket ./forgebucket
EXPOSE 8080
ENTRYPOINT ["./forgebucket"]
