# Build argument pour choisir le mode
ARG BUILD_MODE=production

# Build stage pour production uniquement
FROM node:18-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
COPY prod.env .env
RUN npm run build

# Development stage
FROM node:18-alpine AS development
ENV NODE_ENV=development
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# Ajout de la surveillance des fichiers pour le hot reload
ENV WATCHPACK_POLLING=true
ENV CHOKIDAR_USEPOLLING=true
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]

# Production stage
FROM node:18-alpine AS production
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/messages ./messages
COPY docker/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
RUN chown 1001:1001 -R /app
USER nextjs
ENV HOSTNAME="0.0.0.0"
EXPOSE 3000
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["node", "server.js"]

# Final stage selection
FROM ${BUILD_MODE}
