# CI passes Gitea-hosted dyolink-node (node + dumb-init). See seed-gitea-base-images.ps1.
ARG NODE_IMAGE=node:20-bookworm-slim

# ============================================
# STAGE 1: BUILDER STAGE
# ============================================
FROM ${NODE_IMAGE} AS builder

# CI passes Verdaccio (http://host.docker.internal:4873). Default is official npm for laptop builds.
ARG NPM_REGISTRY=https://registry.npmjs.com

WORKDIR /app

COPY package*.json ./
COPY .npmrc ./
COPY prisma ./prisma/

# Debian bookworm-slim (glibc). Single npm ci + BuildKit cache.
# CI: Verdaccio on :4873 (online fills cache from npmjs; blackout serves disk).
# Does not change package-lock.json in git.
ENV NODE_OPTIONS=--max-old-space-size=3072
RUN --mount=type=cache,target=/root/.npm \
    npm config set registry "${NPM_REGISTRY}" \
 && npm config set fetch-retries 5 \
 && npm config set fetch-retry-maxtimeout 120000 \
 && npm config set maxsockets 5 \
 && sed -i \
      -e "s|https://registry.npmmirror.com|${NPM_REGISTRY}|g" \
      -e "s|https://registry.npmjs.org|${NPM_REGISTRY}|g" \
      -e "s|https://registry.npmjs.com|${NPM_REGISTRY}|g" \
      -e "s|http://host.docker.internal:4873|${NPM_REGISTRY}|g" \
      package-lock.json \
 && npm ci --no-audit --no-fund \
 && test -e node_modules/.bin/prisma \
 && test -e node_modules/.bin/nest

COPY . .

RUN npm run prisma:generate
RUN npm run build
RUN npm prune --omit=dev


# ============================================
# STAGE 2: PRODUCTION STAGE
# ============================================
FROM ${NODE_IMAGE}

# dyolink-node already has dumb-init. Vanilla node: apt-get while online only.
USER root
RUN if ! command -v dumb-init >/dev/null 2>&1; then \
      apt-get update \
      && apt-get install -y --no-install-recommends dumb-init \
      && rm -rf /var/lib/apt/lists/*; \
    fi

WORKDIR /app

RUN groupadd --system --gid 1001 nodejs \
 && useradd --system --uid 1001 --gid nodejs --home-dir /app --shell /usr/sbin/nologin dyolink

# Prisma client comes from the builder (npm prune --production keeps @prisma/client).
COPY package*.json ./
COPY prisma ./prisma/

COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules

COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
# Windows git/build context may use CRLF; strip before chmod (fixes dumb-init "No such file or directory").
RUN sed -i 's/\r$//' /usr/local/bin/docker-entrypoint.sh && chmod +x /usr/local/bin/docker-entrypoint.sh

# AdminJS writes/serves the custom-components bundle here. Avoid the default
# ".adminjs" path — Express sendFile + nosniff can 500 on dot-directories.
ENV ADMIN_JS_TMP_DIR=/app/adminjs-tmp

RUN mkdir -p /app/logs /app/adminjs-tmp && \
    chown -R dyolink:nodejs /app

USER dyolink

EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=5s --start-period=40s --retries=3 \
  CMD node -e "require('http').get('http://127.0.0.1:3000/api/health', (r) => {if(r.statusCode!==200)process.exit(1)})"

ENTRYPOINT ["dumb-init", "--", "docker-entrypoint.sh"]

CMD ["node", "dist/src/main.js"]
