FROM node:12-buster AS node-install

# Fix EOL apt sources and install git
RUN set -eux; \
  cp /etc/apt/sources.list /etc/apt/sources.list.dist || true; \
  printf '%s\n' \
    'deb http://archive.debian.org/debian buster main contrib non-free' \
    'deb http://archive.debian.org/debian buster-updates main contrib non-free' \
    'deb http://archive.debian.org/debian-security buster/updates main contrib non-free' \
    > /etc/apt/sources.list; \
  echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/99no-check-valid-until; \
  apt-get -o Acquire::Check-Valid-Until=false update; \
  apt-get install -y --no-install-recommends git ca-certificates; \
  rm -rf /var/lib/apt/lists/*

RUN git --version

# Project env (skips version.js in postinstall per your comment)
ENV ENV_IS_DOCKER=true

WORKDIR /app
COPY package.json package-lock.json isDockerEnv.js ./

# ---- Optional: Azure Artifacts auth
# Pass these in your pipeline only if you need the private feed:
#   --build-arg FEED_URL=https://pkgs.dev.azure.com/<org>/<project>/_packaging/<feed>/npm/registry
#   --build-arg PAT=$(System.AccessToken)   # or a PAT with Packaging read
ARG FEED_URL=""
ARG PAT=""
RUN set -eux; \
  if [ -n "$FEED_URL" ] && [ -n "$PAT" ]; then \
    host="$(echo "$FEED_URL" | sed -E 's|^https?://||')"; \
    printf 'registry=%s/\n//%s/:_authToken=%s\nalways-auth=true\n' "$FEED_URL" "$host" "$PAT" > .npmrc; \
  fi

# Install deps and ensure node-sass binary matches Node 12
RUN npm install --before=2025-09-1 --unsafe-perm \
 && npm rebuild node-sass || true

# ---- Build
FROM node-install AS node-build
#Copy React Project Files
COPY . .
#Generate version.ts file and run production build
RUN node version.js && npm run build

FROM node-build AS node-test
#Set label to reference this container from pipeline to get test results
LABEL test=true
RUN npm run test-ci;

#Nginx layer for serving build
FROM nginx:stable-alpine AS nginx
#Set working directory to default nginx html directory
WORKDIR /usr/share/nginx/html
#Remove default assests from base directory
RUN rm -rf ./*
#Copy build folder to nginx working directory
COPY --from=node-build /app/build .
#Copy Nginx Config From Project Files.
COPY --from=node-build /app/nginx.conf /etc/nginx/conf.d/default.conf
ENTRYPOINT ["nginx","-g","daemon off;"]
