ARG NGINX_VERSION
ARG NODE_VERSION
ARG NPM_VERSION

# ── Stage: frontend — baut das TypeScript-JS-Bundle ──────────────
# Node.js, npm und node_modules landen NICHT im Prod-Image.
FROM node:${NODE_VERSION}-bookworm-slim AS frontend
# Zielverzeichnis fuer rollup-Output erstellen (absoluter Pfad wie in rollup.config.mjs)
RUN mkdir -p /usr/opt/pythonapp/NGINX_Server/html/static
WORKDIR /usr/opt/pythonapp/Client
# Erst package.json + lockfile kopieren (besseres Layer-Caching)
COPY Client/package.json Client/package-lock.json* ./
RUN npm ci || npm install
# Rest des Client-Quellcodes kopieren
COPY Client/ ./
# config.ini wird fuer rollup benoetigt (inireader.mjs liest Version/Name)
COPY App_Server/root/config.ini /usr/opt/pythonapp/App_Server/root/config.ini
# tsc + rollup ausfuehren — Output geht nach /usr/opt/pythonapp/NGINX_Server/html/static/
RUN npm run build

# ── Stage: nginx ────────────────────────────────────────────────
FROM nginx:${NGINX_VERSION}
RUN ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
RUN mkdir -p /etc/certs /etc/nginx/templates
COPY NGINX_Server/10-select-ssl.sh /docker-entrypoint.d/10-select-ssl.sh
RUN chmod +x /docker-entrypoint.d/10-select-ssl.sh
# NGINX Config-Templates ins Image kopieren (in Prod; Dev ueberschreibt per bind-mount)
COPY NGINX_Server/configtemplates/ /etc/nginx/configtemplates/
# Alle statischen Dateien aus dem Repo kopieren (CSS, favicon, fonts, logo)
COPY NGINX_Server/html/static/ /usr/share/nginx/html/static/
# JS-Bundle aus der frontend-Stage ueberschreibt ggf. vorhandene alte JS-Dateien
COPY --from=frontend /usr/opt/pythonapp/NGINX_Server/html/static/ /usr/share/nginx/html/static/