# =========================================================
# Stage 1: Build Frontend
# =========================================================
FROM node:20-alpine AS builder

WORKDIR /build

# Copier package files depuis frontend-prep
COPY frontend/frontend-prep/package*.json ./

# Installer les dépendances
RUN npm ci

# Copier le code source
COPY frontend/frontend-prep/ .

# Build
RUN npm run build

# =========================================================
# Stage 2: Nginx + ModSecurity
# =========================================================
FROM owasp/modsecurity-crs:nginx-alpine

USER root

# Copier build frontend
COPY --from=builder /build/dist /usr/share/nginx/html

#COPY docker/frontend/cert/cert.pem /etc/nginx/certs/cert.pem
#COPY docker/frontend/cert/key.pem /etc/nginx/certs/key.pem
#RUN chown root:nginx /etc/nginx/certs/cert.pem /etc/nginx/certs/key.pem && \
#    chmod 644 /etc/nginx/certs/cert.pem && \
#    chmod 640 /etc/nginx/certs/key.pem

# Logs ModSecurity
RUN mkdir -p /var/log/modsec && chown -R nginx:nginx /var/log/modsec

COPY docker/docker/frontend/nginx.conf /etc/nginx/conf.d/app.conf
COPY docker/docker/frontend/custom-rules.conf /etc/nginx/modsec/custom-rules.conf
RUN echo "Include /etc/nginx/modsec/custom-rules.conf" > /etc/nginx/modsec/custom-includes.conf

RUN rm -f /etc/nginx/templates/conf.d/default.conf.template || true
RUN chown -R nginx:nginx /usr/share/nginx/html

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