# Build context: repo root (Dockerfile liest aus gerbil-manager-web/).
# docker build -f gerbil-manager-web/Dockerfile -t gerbilmanager-frontend .
#
# Produktion: kein VITE_API_BASE_URL nötig — client.ts fällt auf '/api' zurück,
# nginx proxyt /api/* transparent zum API-Container (keine Host-Kopplung zur Build-Zeit).

FROM node:22-alpine AS build
WORKDIR /app
COPY gerbil-manager-web/package.json gerbil-manager-web/package-lock.json ./
RUN npm ci
COPY gerbil-manager-web/ .
RUN npm run build

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

# nginx: SPA-Fallback + Reverse-Proxy für /api, /scalar, /openapi zum API-Container.
RUN printf 'server {\n\
    listen 80;\n\
    root /usr/share/nginx/html;\n\
    index index.html;\n\
\n\
    # API-Aufrufe: /api/* -> API-Container /* (Praefix wird entfernt)\n\
    location /api/ {\n\
        proxy_pass http://api:8080/;\n\
        proxy_set_header Host $host;\n\
        proxy_set_header X-Real-IP $remote_addr;\n\
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\
    }\n\
\n\
    # Scalar API-Doku und OpenAPI-Spec direkt vom Backend\n\
    location /scalar {\n\
        proxy_pass http://api:8080/scalar;\n\
        proxy_set_header Host $host;\n\
    }\n\
    location /openapi/ {\n\
        proxy_pass http://api:8080/openapi/;\n\
        proxy_set_header Host $host;\n\
    }\n\
\n\
    # SPA-Fallback: alle anderen Pfade laden index.html (React Router)\n\
    location / {\n\
        try_files $uri $uri/ /index.html;\n\
    }\n\
}\n' > /etc/nginx/conf.d/default.conf

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