# ---- Build stage -----------------------------------------------------
FROM node:22-alpine AS build

WORKDIR /app

# Backend URLs are baked in at build time (Vite inlines VITE_* variables).
ARG VITE_API_BASE=https://me-system-api.johannesbot.de
ARG VITE_WS_BASE=wss://me-system-api.johannesbot.de
ENV VITE_API_BASE=$VITE_API_BASE
ENV VITE_WS_BASE=$VITE_WS_BASE

COPY package*.json ./
RUN npm install

COPY . .
RUN npm run build

# Production stage
FROM nginx:stable-alpine as production-stage
# Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*
# Copy built files
COPY --from=build /app/dist /usr/share/nginx/html

# openssl is needed to generate the self-signed certificate on first start.
RUN apk add --no-cache openssl

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY docker/40-self-signed-ssl.sh /docker-entrypoint.d/40-self-signed-ssl.sh
RUN chmod +x /docker-entrypoint.d/40-self-signed-ssl.sh

COPY --from=build /app/dist /usr/share/nginx/html

EXPOSE 443

# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
