FROM node:20-bookworm-slim AS server-deps
WORKDIR /app/server

COPY server/package.json server/package-lock.json ./
COPY server/scripts ./scripts
COPY server/prisma ./prisma

RUN npm ci --no-audit --no-fund


FROM server-deps AS server-build
WORKDIR /app/server

COPY server/tsconfig.json ./tsconfig.json
COPY server/src ./src

RUN npm run build


FROM node:20-bookworm-slim AS server-runtime
WORKDIR /app/server

COPY server/package.json server/package-lock.json ./
COPY server/scripts ./scripts
COPY server/prisma ./prisma

RUN apt-get update \
  && apt-get install -y --no-install-recommends openssh-client \
  && rm -rf /var/lib/apt/lists/*

RUN npm ci --omit=dev --no-audit --no-fund

COPY --from=server-build /app/server/dist ./dist

ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=8787
EXPOSE 8787

CMD ["npm", "run", "start"]


FROM node:20-bookworm-slim AS web-deps
WORKDIR /app/web

COPY web/package.json web/package-lock.json ./
RUN npm ci --no-audit --no-fund


FROM web-deps AS web-build
WORKDIR /app/web

COPY web/ ./

ARG VITE_API_BASE_URL=/api
ARG VITE_ADMIN_TOKEN=
ENV VITE_API_BASE_URL=${VITE_API_BASE_URL}
ENV VITE_ADMIN_TOKEN=${VITE_ADMIN_TOKEN}

RUN npm run build


FROM nginx:1.27-alpine AS web-runtime

COPY --from=web-build /app/web/dist /usr/share/nginx/html
COPY dist/default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80
