# Frontend - Multi-stage optimizado
FROM node:22-alpine AS build
WORKDIR /app

# Aumentar memoria para build Angular
ENV NODE_OPTIONS="--max-old-space-size=4096"

# Cache de dependencias - solo package files primero
COPY src/frontend/package.json src/frontend/package-lock.json* ./

# Instalar con cache
RUN npm ci --legacy-peer-deps --prefer-offline

# Copiar código y compilar
COPY src/frontend/ .
RUN npm run build -- --configuration production

# Runtime con Nginx optimizado
FROM nginx:alpine AS runtime

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

# Copiar archivos compilados
COPY --from=build /app/dist/student-enrollment/browser /usr/share/nginx/html

# Configuración nginx optimizada
COPY deploy/docker/nginx.conf /etc/nginx/conf.d/default.conf

# Usuario no-root
RUN chown -R nginx:nginx /usr/share/nginx/html && \
    chown -R nginx:nginx /var/cache/nginx && \
    chown -R nginx:nginx /var/log/nginx && \
    touch /var/run/nginx.pid && \
    chown nginx:nginx /var/run/nginx.pid

EXPOSE 80

HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
  CMD curl -f http://localhost/ || exit 1

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