# Reproducible source/patch inputs and the base versions match Element v1.12.35.
FROM --platform=linux/amd64 ghcr.io/pnpm/pnpm:12.14.0@sha256:71202ae73719148f6b6e129ca1e9fa3ceabbc9a037dc34800715606f2b5290a1 AS pnpm
FROM --platform=linux/amd64 docker.io/library/node:24-bullseye@sha256:25f3016fcdae6b5d65bd9bcb4064b7e4198ec8d49493fa40d73f9b463d04fb15 AS builder
COPY --from=pnpm /opt/pnpm /opt/pnpm
RUN ln -s /opt/pnpm/pnpm /usr/local/bin/pnpm
WORKDIR /src
COPY --from=upstream / /src/
ARG PENTA_DIST_VERSION
ENV CI=true HUSKY=0 NX_DAEMON=false NX_PARALLEL=1 NX_SKIP_NX_CACHE=true \
    NX_SKIP_REMOTE_CACHE=true NX_CLOUD=false NX_TASKS_RUNNER_DYNAMIC_OUTPUT=false \
    NODE_OPTIONS=--max-old-space-size=3072 RAYON_NUM_THREADS=2 \
    PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 ELECTRON_SKIP_BINARY_DOWNLOAD=1
RUN pnpm install --frozen-lockfile --network-concurrency=4 --child-concurrency=1
RUN VERSION="$PENTA_DIST_VERSION" pnpm --dir apps/web build --parallel=1
# CHAT-SPRINT-003: exercise real participant transitions in the pinned toast
# suite after Nx has built its workspace dependencies. One worker, no browser.
RUN pnpm --dir apps/web exec vitest run src/toasts/IncomingCallToast.test.tsx src/hooks/useRoomCall.test.ts \
    src/penta_delivery/PentaDeliveryReceipts.test.ts \
    src/penta/enrollmentRoom.test.ts \
    --project=unit --maxWorkers=1 --no-file-parallelism
RUN test -s /src/apps/web/webapp/index.html && test -s /src/apps/web/webapp/sw.js \
    && test -s /src/apps/web/webapp/penta-branding/penta-chat-192.png

# Depend on the completed Web builder so its Node jobs cannot overlap Call's.
# Call keeps its own frozen dependencies and exact upstream package manager.
FROM builder AS call_builder
WORKDIR /element-call
COPY --from=upstream /penta-call-source/ /element-call/
RUN node --input-type=module -e '\
    import { readFileSync, writeFileSync } from "node:fs"; \
    import { createHash } from "node:crypto"; \
    const manager = JSON.parse(readFileSync("package.json", "utf8")).packageManager; \
    const match = /^pnpm@11\.21\.0\+sha512\.([0-9a-f]{128})$/.exec(manager); \
    if (!match) throw new Error("Unpinned Call package manager"); \
    const response = await fetch("https://registry.npmjs.org/pnpm/-/pnpm-11.21.0.tgz", { redirect: "error", signal: AbortSignal.timeout(120000) }); \
    if (!response.ok || Number(response.headers.get("content-length")) > 16777216) throw new Error("Call pnpm transfer failed"); \
    const chunks = []; let size = 0; \
    for await (const chunk of response.body) { size += chunk.length; if (size > 16777216) throw new Error("Call pnpm transfer exceeds bound"); chunks.push(chunk); } \
    const raw = Buffer.concat(chunks); \
    if (!raw.length || createHash("sha512").update(raw).digest("hex") !== match[1]) throw new Error("Call pnpm integrity differs"); \
    writeFileSync("/tmp/penta-call-pnpm.tgz", raw);'
RUN mkdir /opt/penta-call-pnpm
RUN tar -xzf /tmp/penta-call-pnpm.tgz --strip-components=1 -C /opt/penta-call-pnpm
RUN node -e 'if (require("/opt/penta-call-pnpm/package.json").version !== "11.21.0") throw new Error("Call pnpm version differs")'
RUN node /opt/penta-call-pnpm/bin/pnpm.cjs install --frozen-lockfile --network-concurrency=4 --child-concurrency=1
RUN node /opt/penta-call-pnpm/bin/pnpm.cjs exec vitest run --project=unit --maxWorkers=1 --no-file-parallelism \
    src/components/CallFooterViewModel.test.ts src/components/CallFooter.device-errors.test.tsx \
    src/components/MicrophoneLevelMeter.test.tsx src/state/MicrophoneLevel.test.ts \
    src/UrlParams.test.ts src/state/MediaDevices.test.ts src/e2ee/sharedKeyManagement.test.ts \
    sdk/src/media-backend/livekit/Connection.test.ts src/room/CallView.test.tsx
ARG PENTA_CALL_DIST_VERSION
ENV VITE_APP_VERSION=$PENTA_CALL_DIST_VERSION VITE_PRODUCT_NAME="Penta Chat"
# The upstream package script requests 16GiB. Invoke the same embedded Vite
# entry through the source-offered wrapper with a hard 3072MiB Node heap.
RUN node --max-old-space-size=3072 node_modules/vite/bin/vite.js build --config penta-embedded-vite.config.mjs
RUN test -s /element-call/dist/index.html && test -d /element-call/dist/assets

# Keep the same Nginx/entrypoint/healthcheck contract as the accepted image.
# Old hashed bundles remain readable for an already-open tab until normal reload;
# the new index, manifest, logos and all vector-icon aliases are overwritten.
FROM --platform=linux/amd64 ghcr.io/element-hq/element-web:v1.12.35@sha256:406b48b80153818284a235fa3600b2a5b5b6b0aa0e4ef4896b36d8d3461352f3
USER root
COPY --from=builder /src/apps/web/webapp /app
COPY --from=call_builder /element-call/dist /app/widgets/element-call
# The existing DEV Compose mounts its reviewed public /app/config.json. Fail
# closed without that mount instead of inheriting upstream's matrix.org sample.
RUN rm -f /app/config.json
ARG PENTA_UPSTREAM_COMMIT
ARG PENTA_PATCHSET_SHA256
ARG PENTA_PATCHED_SOURCE_SHA256
ARG PENTA_SOURCE_REVISION
ARG PENTA_CALL_UPSTREAM_COMMIT
ARG PENTA_CALL_PATCHSET_SHA256
ARG PENTA_CALL_PATCHED_SOURCE_SHA256
LABEL org.opencontainers.image.title="Penta Chat" \
      org.opencontainers.image.source="https://github.com/element-hq/element-web" \
      org.opencontainers.image.revision="$PENTA_UPSTREAM_COMMIT" \
      org.penta-chat.patchset-sha256="$PENTA_PATCHSET_SHA256" \
      org.penta-chat.patched-source-sha256="$PENTA_PATCHED_SOURCE_SHA256" \
      org.penta-chat.source-revision="$PENTA_SOURCE_REVISION" \
      org.penta-chat.call-upstream-commit="$PENTA_CALL_UPSTREAM_COMMIT" \
      org.penta-chat.call-patchset-sha256="$PENTA_CALL_PATCHSET_SHA256" \
      org.penta-chat.call-patched-source-sha256="$PENTA_CALL_PATCHED_SOURCE_SHA256"
USER nginx
