# syntax=docker/dockerfile:1.7

FROM node:24-alpine AS builder

WORKDIR /build

COPY package.json package-lock.json ./
RUN npm ci --ignore-scripts

COPY . .

# O repositório preserva adapter-auto para desenvolvimento. A imagem troca o
# adapter somente dentro do build, gerando um SPA estático servido pelo Nginx.
RUN npm install --no-save --ignore-scripts @sveltejs/adapter-static@3.0.10 \
    && sed -i \
        -e 's#@sveltejs/adapter-auto#@sveltejs/adapter-static#' \
        -e "s#adapter: adapter()#adapter: adapter({ fallback: 'index.html' })#" \
        vite.config.ts \
    && npm run prepare

ARG VITE_API_URL=
ARG VITE_API_POLL_INTERVAL=6000
ARG VITE_DEMO_MODE=false
ENV VITE_API_URL=${VITE_API_URL} \
    VITE_API_POLL_INTERVAL=${VITE_API_POLL_INTERVAL} \
    VITE_DEMO_MODE=${VITE_DEMO_MODE}

RUN npm run build && test -f build/index.html

FROM nginx:1.29-alpine AS runtime

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /build/build /usr/share/nginx/html

RUN nginx -t

EXPOSE 8080

HEALTHCHECK --interval=15s --timeout=3s --start-period=5s --retries=5 \
    CMD wget --quiet --output-document=/dev/null http://127.0.0.1:8080/health || exit 1
