FROM node:20-slim as build-stage

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

RUN corepack enable

WORKDIR /app

COPY . .

ENV VITE_API_BASE_URL=ENV_API_BASE_URL
ENV VITE_KC_URL=ENV_KC_URL
ENV VITE_KC_REALM=ENV_KC_REALM
ENV VITE_KC_CLIENT_ID=ENV_KC_CLIENT_ID
ENV VITE_EDM_MICRO_FRONTEND_URL=ENV_EDM_MICRO_FRONTEND_URL
ENV VITE_API_BASE_URL_OCR=ENV_API_BASE_URL_OCR

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run build

FROM alpine as production-stage

WORKDIR /app

RUN apk add miniserve --repository=https://dl-cdn.alpinelinux.org/alpine/edge/community

COPY --from=build-stage /app/dist/spa .
COPY --from=build-stage /app/entrypoint.sh ./entrypoint.sh

RUN chmod u+x ./entrypoint.sh

ENTRYPOINT ["./entrypoint.sh"]
