# Shared copy-only product-backend image for the "bundle" products
# (probe / funke-pegasus / norsk-commentary / norsk-playout). Single-sourced in
# @norskvideo/ctl-dev-kit so the four images cannot drift; the per-product facts
# (tag, container port, optional native addon / assets) come in as build args and
# staged directories from the shared build-bundle-image.sh driver.
#
# Studio is NOT built from this file — it ships a `bun build --compile` binary
# with a different (STUDIO_ASSET_ROOT) layout; see products/studio/deployment.
#
# Copy-only: the backend bundle, frontend, dashboards, components, and the
# optional native addon + product-template assets are all built/collected on the
# host by the driver and COPYd in. Nothing is installed or compiled here (the old
# per-product images ran `bun install` inside an oven/bun:alpine builder and
# shipped the whole node_modules; the bundle inlines only what it reaches).
#
# The base is single-sourced in build/common.sh (PRODUCT_BASE_IMAGE) and passed
# as BASE_IMAGE, so every product image shares one pinned distroless base (glibc
# + libgcc/libstdc++, no shell). The runtime bun is imported Docker-natively from
# the upstream slim image (BUN_IMAGE = oven/bun:<version>-slim): buildkit rejects
# a variable in `COPY --from=${BUN_IMAGE}`, so it's aliased to a named stage from
# the global-scope ARG and the binary COPYd out; that stage only donates a
# prebuilt binary, so the final image stays copy-only.
#
# Split-level asset layout (the subtle part): the backend is a single bundled
# file at backend/dist/index.js, so at runtime import.meta.dir is that bundle's
# dir for ALL of the collapsed modules. server.ts's `../../frontend/dist` lands
# beside the app dir, but lib/{components,dashboards,assets}.ts (originally under
# backend/src/lib, one level deeper) kept `../../../{components/lib,dashboards,
# assets}`, which resolve ONE level above the app dir. So dashboards +
# components/lib + assets go at /usr/src while backend/frontend go under
# /usr/src/app. A flat /usr/src/app layout silently drops them from the generated
# product-template tars.
ARG BASE_IMAGE
ARG BUN_IMAGE
FROM ${BUN_IMAGE} AS bun

FROM ${BASE_IMAGE}
# The container port docker-runner maps the host port to (probe/funke 4321,
# commentary/playout 4322). Passed by the driver from the wrapper's PRODUCT_PORT.
ARG PRODUCT_PORT=4321
COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun
WORKDIR /usr/src/app
COPY backend/dist ./backend/dist
# Optional native addon(s), beside the bundle where <product>-identity.ts
# createRequires them. The driver stages an empty native/ for products that ship
# no signer (funke/commentary/playout), so this COPY is then a harmless no-op.
COPY native/ ./backend/dist/
COPY frontend/dist ./frontend/dist
COPY dashboards /usr/src/dashboards
COPY fleet-dashboards /usr/src/fleet-dashboards
COPY components/lib /usr/src/components/lib
# Optional product-template assets (funke's slate/black PNGs). Empty otherwise.
COPY assets /usr/src/assets
# The docs bundle (docs/generated/bundle/ on the host), served by the product
# at /docs. Beside backend/ and frontend/ so server.ts reaches it the same way
# it reaches frontend/dist (../../docs from the bundle dir). Empty when the
# product built none.
COPY docs ./docs
ENV PORT=${PRODUCT_PORT}
EXPOSE ${PRODUCT_PORT}
CMD ["/usr/local/bin/bun", "run", "backend/dist/index.js"]
