# Multi-stage production Dockerfile for gameno-api

# ------------------------------------------------------------------------------
# Stage 1: Install production dependencies
# ------------------------------------------------------------------------------
FROM node:22-alpine AS dependencies

WORKDIR /app

COPY package*.json .npmrc* ./

RUN npm ci --omit=dev --registry=https://registry.npmmirror.com && npm cache clean --force

# ------------------------------------------------------------------------------
# Stage 2: Production runtime image
# ------------------------------------------------------------------------------
FROM node:22-alpine AS production

WORKDIR /app

ENV NODE_ENV=production \
    PORT=3000

# Copy installed production dependencies and application code with proper ownership
COPY --chown=node:node --from=dependencies /app/node_modules ./node_modules
COPY --chown=node:node . .

# Run as non-root user
USER node

EXPOSE 3000

# Healthcheck targeting the API health endpoint
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1

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