FROM node:22-alpine AS base

WORKDIR /app

RUN apk add --no-cache bash curl python3 py3-pip

FROM base AS client_deps
COPY client/package.json client/package-lock.json ./client/
RUN cd client && npm ci

FROM client_deps AS client_build
ARG VITE_BUILD_SHA=unknown
ENV VITE_BUILD_SHA=$VITE_BUILD_SHA
COPY client ./client
RUN cd client && npm run build

FROM base AS server_deps
COPY server/package.json server/package-lock.json ./server/
COPY server/requirements/payment-order-biff8.txt ./server/requirements/payment-order-biff8.txt
COPY server/prisma ./server/prisma
RUN python3 -m venv /opt/payment-order-venv \
    && /opt/payment-order-venv/bin/pip install --disable-pip-version-check --no-cache-dir --require-hashes -r server/requirements/payment-order-biff8.txt
RUN cd server && npm ci

FROM server_deps AS server_build
ARG BUILD_SHA=unknown
ENV BUILD_SHA=$BUILD_SHA
COPY server ./server
COPY scripts ./scripts
COPY --from=client_build /app/client/dist ./client/dist

WORKDIR /app/server
ENV NODE_ENV=production
ENV PAYMENT_ORDER_PYTHON_BIN=/opt/payment-order-venv/bin/python
CMD ["npm", "start"]
