# Multi-stage workspace-aware Dockerfile for Module Federation packages
# This Dockerfile is designed to build federated modules that depend on workspace packages
# Build from repository root: docker build --file ./packages/<module>/Dockerfile.workspace .

# Source code arguments - can be overridden for different modules
ARG MODULE_NAME=mod-arch
ARG UI_SOURCE_CODE=./packages/${MODULE_NAME}/frontend
ARG BFF_SOURCE_CODE=./packages/${MODULE_NAME}/bff

# Set the base images for the build stages
ARG NODE_BASE_IMAGE=registry.access.redhat.com/ubi9/nodejs-22:latest
ARG GOLANG_BASE_IMAGE=registry.access.redhat.com/ubi9/go-toolset:1.26
ARG DISTROLESS_BASE_IMAGE=registry.access.redhat.com/ubi9-minimal:latest

# UI build stage
FROM ${NODE_BASE_IMAGE} AS ui-builder

ARG UI_SOURCE_CODE
ARG MODULE_NAME
ARG DEPLOYMENT_MODE=federated
# Promote the build arg to an env var so the frontend build compiles in federated
# Module Federation mode by default (host provides shared @odh-dashboard/* + framework packages).
ENV DEPLOYMENT_MODE=${DEPLOYMENT_MODE}
ARG PLATFORM_MODE

WORKDIR /usr/src/workspace

# Install the pinned pnpm CLI from the npm-prefetched bootstrap package.
COPY --chown=default:root prefetch/pnpm/package.json prefetch/pnpm/package-lock.json ./prefetch/pnpm/

### Workspace setup
# Copy root workspace manifest and lockfile for pnpm workspace resolution
COPY --chown=default:root package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY --chown=default:root .npmrc ./
# Copy the frontend workspace manifest so its dependencies can be resolved if referenced
COPY --chown=default:root frontend/package.json ./frontend/
# Copy required shared workspace packages needed for module builds
COPY --chown=default:root packages/app-config/ ./packages/app-config/
COPY --chown=default:root packages/foundation/ ./packages/foundation/
COPY --chown=default:root packages/k8s-core/ ./packages/k8s-core/
COPY --chown=default:root packages/plugin-core/ ./packages/plugin-core/
COPY --chown=default:root packages/tsconfig/ ./packages/tsconfig/
COPY --chown=default:root packages/ui-core/ ./packages/ui-core/
# Copy only the frontend source (not the entire frontend build artifacts) for cross-module imports
COPY --chown=default:root frontend/src/ ./frontend/src/
# Copy build scripts required by webpack/jest configs
COPY --chown=default:root scripts/ ./scripts/

# Copy the specific module source code
COPY --chown=default:root ${UI_SOURCE_CODE} ./${UI_SOURCE_CODE}

# Change file ownership to the assemble user
USER default

# Install pnpm and workspace dependencies (@odh-dashboard/* packages)
ENV PATH="/usr/src/workspace/prefetch/pnpm/node_modules/.bin:${PATH}"
RUN npm ci --prefix ./prefetch/pnpm --prefer-offline --ignore-scripts --no-audit --no-fund --no-progress \
    && test "$(pnpm --version)" = "11.22.0"
RUN CYPRESS_INSTALL_BINARY=0 pnpm install --frozen-lockfile --prefer-offline

# Build the module frontend from the shared pnpm workspace.
WORKDIR /usr/src/workspace/${UI_SOURCE_CODE}
RUN pnpm run build:prod

# BFF build stage
FROM ${GOLANG_BASE_IMAGE} AS bff-builder

ARG BFF_SOURCE_CODE
ARG TARGETOS
ARG TARGETARCH

WORKDIR /usr/src/app

# Copy the Go Modules manifests
COPY ${BFF_SOURCE_CODE}/go.mod ${BFF_SOURCE_CODE}/go.sum ./

# Download dependencies
RUN go mod download

# Copy the go source files
COPY ${BFF_SOURCE_CODE}/cmd/ cmd/
COPY ${BFF_SOURCE_CODE}/internal/ internal/

# Build the Go application
RUN CGO_ENABLED=1 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -ldflags="-s -w" -tags strictfipsruntime -o bff ./cmd

# Final stage
FROM ${DISTROLESS_BASE_IMAGE}

ARG UI_SOURCE_CODE

WORKDIR /
COPY --from=bff-builder /usr/src/app/bff ./
COPY --from=ui-builder /usr/src/workspace/${UI_SOURCE_CODE}/dist ./static/
USER 65532:65532

# Set environment variables
# Expose port 8080
EXPOSE 8080

ENTRYPOINT ["/bff"]
