# =============================================================================
# RecreaHUB API — Multi-stage Dockerfile
# Stage 1 (deps):   install production dependencies
# Stage 2 (runner): lean production image with non-root user
# =============================================================================

# ── Stage 1: Install dependencies ────────────────────────────────────────────
FROM node:20-alpine AS deps

WORKDIR /app

# Copy only package manifests first for better layer caching
COPY package.json package-lock.json ./

# Install production dependencies (no devDependencies)
RUN npm ci --omit=dev


# ── Stage 2: Production runner ────────────────────────────────────────────────
FROM node:20-alpine AS runner

# Install curl for HEALTHCHECK
RUN apk add --no-cache curl

WORKDIR /app

# Run as the built-in non-root 'node' user for security
USER node

# Copy installed modules from the deps stage
COPY --from=deps --chown=node:node /app/node_modules ./node_modules

# Copy application source
COPY --chown=node:node . .

EXPOSE 3000

# Health check: verify the /health endpoint responds within 30 s
HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
    CMD curl -f http://localhost:3000/health || exit 1

CMD ["node", "server.js"]
