FROM node:22-alpine AS build

WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY . .

ARG VITE_PACKAGE_BASE_URL=
ARG VITE_AGENT_BASE_URL=http://127.0.0.1:5010
ARG VITE_APP_OPEN_URL=http://127.0.0.1

RUN VITE_PACKAGE_BASE_URL="${VITE_PACKAGE_BASE_URL}" \
  VITE_AGENT_BASE_URL="${VITE_AGENT_BASE_URL}" \
  VITE_APP_OPEN_URL="${VITE_APP_OPEN_URL}" \
  npm run build

FROM nginx:1.27-alpine AS runtime

ENV PACKAGE_PROXY_TARGET=http://web-server:3000

COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf.template /etc/nginx/templates/default.conf.template

EXPOSE 80

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD wget -qO- http://127.0.0.1/ >/dev/null || exit 1
