FROM node:18 AS base
WORKDIR /usr/local/app

################  FRONTEND BUILD  #################
FROM base AS client-build
WORKDIR /usr/local/app/frontend
COPY SerpentRace_Frontend/package.json SerpentRace_Frontend/package-lock.json ./
RUN npm ci
COPY SerpentRace_Frontend/ ./
RUN npm run build

################  BACKEND BUILD  #################
FROM base AS backend-build
WORKDIR /usr/local/app/backend
COPY SerpentRace_Backend/package.json SerpentRace_Backend/package-lock.json ./
RUN npm ci
COPY SerpentRace_Backend/ ./
# Copy frontend build output to backend static directory
COPY --from=client-build /usr/local/app/frontend/dist ./src/static

################  PRODUCTION IMAGE  #################
FROM backend-build AS prod
WORKDIR /usr/local/app/backend
ENV NODE_ENV=production
RUN npm ci --only=production
EXPOSE 3000
CMD ["node", "src/index.mjs"]