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

WORKDIR /app

# Copier les fichiers de dépendances
COPY frontend/package*.json ./

# Installation propre sans variables d'environnement parasitaires
RUN npm ci

# Copier le code source
COPY frontend/ ./

# Argument pour l'URL de l'API
ARG VITE_API_URL
ENV VITE_API_URL=${VITE_API_URL}

# Build de production
RUN npm run build

# Stage 2: Nginx
FROM nginx:alpine

# Config Nginx
COPY docker/frontend/nginx.conf /etc/nginx/conf.d/default.conf

# Copier les fichiers buildés
COPY --from=builder /app/dist /usr/share/nginx/html

# Script d'entrypoint
COPY docker/frontend/entrypoint.sh /docker-entrypoint.d/40-env-config.sh
RUN chmod +x /docker-entrypoint.d/40-env-config.sh

EXPOSE 80

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
    CMD wget -q --spider http://localhost/health || exit 1