# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npx @mgks/docmd build

# Eliminar mermaid.js para mejorar rendimiento (no se usa)
RUN rm -f ./site/assets/js/mermaid.min.js ./site/assets/js/docmd-mermaid.js && \
    find ./site -name "*.html" -exec sed -i 's|<script src="[^"]*mermaid[^"]*"></script>||g' {} \;

# Copiar robots.txt y sitemap.xml al directorio site
RUN cp robots.txt sitemap.xml ./site/ 2>/dev/null || true

# Stage 2: Serve
FROM nginx:alpine

# Instalar curl para healthcheck
RUN apk add --no-cache curl

COPY --from=builder /app/site /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

# Healthcheck: verificar que nginx responde en /health
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD curl -f http://localhost/health || exit 1

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