FROM --platform=$BUILDPLATFORM node:22-alpine AS build
WORKDIR /app

ARG REGISTRY_TOKEN
COPY package.json package-lock.json .npmrc ./
RUN echo "//gitea.siegeln.net/api/packages/cameleer/npm/:_authToken=${REGISTRY_TOKEN}" >> .npmrc && \
    npm ci

COPY . .

# Upgrade design system to latest dev snapshot (after COPY to bust Docker cache)
RUN echo "//gitea.siegeln.net/api/packages/cameleer/npm/:_authToken=${REGISTRY_TOKEN}" >> .npmrc && \
    npm install @cameleer/design-system@dev && \
    rm -f .npmrc

ARG VITE_ENV_NAME=PRODUCTION
ARG VITE_APP_VERSION=dev
ENV VITE_ENV_NAME=$VITE_ENV_NAME
ENV VITE_APP_VERSION=$VITE_APP_VERSION
RUN npm run build

FROM nginx:1.27-alpine
COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/templates/default.conf.template
COPY docker-entrypoint.sh /cameleer-entrypoint.sh
RUN chmod +x /cameleer-entrypoint.sh

# Default API URL — override via K8s env or docker run -e
ENV CAMELEER_API_URL=http://cameleer-server:8081
# Base path for serving the SPA from a subpath (e.g., /server/). Default: /
ENV BASE_PATH=/

ENTRYPOINT ["/cameleer-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]

EXPOSE 80
