# Build context is the REPO ROOT (see docker-compose.yml): the frontend
# type-imports definitions from backend/src, and frontend drift tests import
# Word add-in sources. A clean build therefore needs both source graphs for
# TypeScript resolution, even though neither belongs in the runtime image.
FROM node:22-slim AS frontend-dependencies
WORKDIR /app
COPY frontend/package*.json ./
RUN npm ci

FROM node:22-slim AS backend-type-dependencies
WORKDIR /backend
COPY backend/package*.json ./
RUN npm ci

FROM frontend-dependencies AS build
ARG NEXT_PUBLIC_SOURCE_CODE_URL
ENV NEXT_PUBLIC_SOURCE_CODE_URL=$NEXT_PUBLIC_SOURCE_CODE_URL
COPY frontend/ ./
COPY backend/src /backend/src
COPY word-addin/src /word-addin/src
COPY --from=backend-type-dependencies /backend/node_modules /backend/node_modules
RUN npm run build

# Keep compiler/test packages and the backend-only type dependency tree out of
# the production image. `next start` needs only frontend production packages,
# the build output, public assets, and the resolved Next configuration.
FROM node:22-slim AS runner
WORKDIR /app
COPY frontend/package*.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY --from=build /app/.next ./.next
COPY --from=build /app/public ./public
COPY --from=build /app/next.config.ts ./next.config.ts

ENV NODE_ENV=production
USER node
EXPOSE 3000
CMD ["npm", "start"]
