FROM node:22-slim AS builder
WORKDIR /app/web

ARG GITEA_NPM_HOST
ENV NODE_TLS_REJECT_UNAUTHORIZED=0
ENV NPM_CONFIG_STRICT_SSL=false
ENV GITEA_NPM_HOST=$GITEA_NPM_HOST

RUN npm config set strict-ssl false \
  && npm install -g pnpm@10.6.5

COPY .npmrc.docker ./.npmrc
COPY .docker-deps/ ../.docker-deps/
COPY web/package.json ./package.json
RUN pnpm install --ignore-scripts --lockfile=false

COPY web/next.config.ts ./next.config.ts
COPY web/tsconfig.json ./tsconfig.json
COPY web/next-env.d.ts ./next-env.d.ts
COPY web/src/ ./src/
COPY shared/ ../shared/

ARG NEXT_PUBLIC_NOTES_API_URL
ARG NEXT_PUBLIC_PLATFORM_SERVICE_URL
ENV NEXT_PUBLIC_NOTES_API_URL=$NEXT_PUBLIC_NOTES_API_URL
ENV NEXT_PUBLIC_PLATFORM_SERVICE_URL=$NEXT_PUBLIC_PLATFORM_SERVICE_URL
ENV NEXT_TELEMETRY_DISABLED=1
RUN pnpm run build

FROM node:22-slim
WORKDIR /app/web
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

COPY --from=builder /app/web/.next/standalone ./
# The Next.js standalone server (at /app/web/web/server.js) serves
# /_next/static/* from a `web/.next/static` directory relative to its
# own location, NOT from /app/web/.next/static. Without this, all
# generated JS chunks 404 with text/plain content-type and the SPA
# never hydrates.
COPY --from=builder /app/web/.next/static ./web/.next/static

EXPOSE 3045
ENV PORT=3045
CMD ["node", "web/server.js"]
