# ---- stage 0: wasm --------------------------------------------------
# The Rust core compiled to the browser module (R2). Same toolchain and wasm-bindgen CLI
# as docker/rust-builder.Dockerfile (keep the two ARG lines equal); scripts/wasm-build.sh
# refuses a CLI that differs from the wasm-bindgen crate in Cargo.lock. The production VPS
# never builds this image: it pulls it by digest (DEPLOY.md).
FROM rust:1.99.0-bookworm AS wasm

ARG WASM_BINDGEN_VERSION=0.2.129

RUN rustup target add wasm32-unknown-unknown \
    && rustup component add rustfmt clippy \
    && cargo install wasm-bindgen-cli --version "${WASM_BINDGEN_VERSION}" --locked \
    && rm -rf /usr/local/cargo/registry

WORKDIR /w
COPY rust-toolchain.toml Cargo.toml Cargo.lock ./
COPY .cargo .cargo
COPY crates crates
COPY scripts/rust-env.sh scripts/wasm-build.sh scripts/
# The build and wasm-bindgen run in one RUN: the cache-mounted target never reaches the
# layer, the generated packages/core-wasm/pkg does.
RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/w/target \
    WASM_BUILD_SHIPPED_ONLY=1 bash scripts/wasm-build.sh

# ---- stage 1: builder -----------------------------------------------
# Node 24 LTS (Node 20 reached end-of-life on 2026-04-30). pnpm pinned to the
# same version as package.json "packageManager"; pnpm 11 needs Node >= 22.13
# and rewrites the lockfile, so the 10.x line stays pinned on purpose.
FROM node:24-alpine AS builder

WORKDIR /app

RUN corepack enable && corepack prepare pnpm@10.13.1 --activate

# Workspace manifests first for layer caching (install depends only on these)
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc ./
COPY apps/app/package.json apps/app/
COPY packages/domain/package.json packages/domain/
COPY packages/anatomy/package.json packages/anatomy/
COPY packages/pdf/package.json packages/pdf/
COPY packages/supabase/package.json packages/supabase/
COPY packages/core-wasm/package.json packages/core-wasm/

RUN pnpm install --frozen-lockfile

# Copy source and build the private app only.
# SHEET_BUNDLE=draft embeds the reference equine DRAFT (medically unvalidated)
# into the bundle for a preview deployment; the default image carries no sheet
# (BDR-037, Step 5 D2): `docker build --build-arg SHEET_BUNDLE=draft .`
ARG SHEET_BUNDLE=none
ENV SHEET_BUNDLE=$SHEET_BUNDLE
COPY . .
COPY --from=wasm /w/packages/core-wasm/pkg packages/core-wasm/pkg
RUN pnpm --filter @faunosteo/app build

# ---- stage 2: runner ------------------------------------------------
FROM nginx:1.31-alpine AS runner

# Copy built static assets; the core module is served precompressed (gzip_static)
COPY --from=builder /app/apps/app/dist /usr/share/nginx/html
RUN gzip -9 -k /usr/share/nginx/html/assets/*.wasm

# Copy custom nginx config and entrypoint
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY docker/entrypoint.sh /entrypoint.sh

# Run fully as non-root (port 8080 is unprivileged). nginx needs to own:
# its runtime dirs, the config it rewrites at startup (PORT envsubst),
# and the html dir (entrypoint writes env.js there).
RUN chmod +x /entrypoint.sh \
    && touch /var/run/nginx.pid \
    && chown -R nginx:nginx /var/cache/nginx /var/run/nginx.pid \
        /etc/nginx/conf.d /usr/share/nginx/html

USER nginx

EXPOSE 8080

ENTRYPOINT ["/entrypoint.sh"]
