# RapidSweeps site (Next.js). Pages are rendered per request from a JSON copy of the data, which is patched from the live APIs.
#   docker build -t swpsnewsite .
#   docker run -p 3000:3000 swpsnewsite

# ── 1. Dependencies ───────────────────────────────────────────────────────────
FROM node:24-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json .npmrc ./
RUN npm ci

# ── 2. Build ──────────────────────────────────────────────────────────────────
FROM node:24-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# .env (copied in with the rest of the source) supplies the NEXT_PUBLIC_* keys, which are baked into the browser bundle.
ENV SITE_URL=https://rapidsweeps.mobi \
    NEXT_TELEMETRY_DISABLED=1
RUN npm run build

# ── 3. Run ────────────────────────────────────────────────────────────────────
FROM node:24-alpine AS run
WORKDIR /app
ENV SITE_URL=https://rapidsweeps.mobi \
    NODE_ENV=production \
    NEXT_TELEMETRY_DISABLED=1 \
    PORT=3000 \
    HOSTNAME=0.0.0.0 \
    DATA_DIR=/app/.data
# Pull in the latest security patches for the system packages in the base image (re-run on every build)
RUN apk upgrade --no-cache
# Only what the server needs: the standalone bundle, hashed assets and the public folder
COPY --from=build --chown=node:node /app/.next/standalone ./
COPY --from=build --chown=node:node /app/.next/static ./.next/static
COPY --from=build --chown=node:node /app/public ./public
COPY --from=build --chown=node:node /app/.env ./.env
# The live copy of the games and bonuses (patched from the APIs on page requests) is saved here
RUN mkdir -p /app/.data && chown node:node /app/.data
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD wget -qO- http://127.0.0.1:3000/robots.txt >/dev/null || exit 1
CMD ["node", "server.js"]
