FROM docker.arvancloud.ir/node:22-alpine AS build

WORKDIR /app

COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm \
    npm ci --no-audit --no-fund

COPY . .

# Vite embeds VITE_* variables in the static bundle during this stage.
ARG VITE_API_URL=https://api.persain-game.com/api/v1
ENV VITE_API_URL=${VITE_API_URL}

RUN npm run build

FROM docker.arvancloud.ir/nginxinc/nginx-unprivileged:1.28-alpine AS runtime

COPY --chown=nginx:nginx nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build --chown=nginx:nginx /app/dist /usr/share/nginx/html
COPY --from=build --chown=nginx:nginx /app/robots.txt /usr/share/nginx/html/robots.txt
COPY --from=build --chown=nginx:nginx /app/sitemap.xml /usr/share/nginx/html/sitemap.xml

USER nginx

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
  CMD wget -q -O /dev/null http://127.0.0.1:8080/healthz || exit 1

CMD ["nginx", "-g", "daemon off;"]
