# XYZ POS Backend - Production Dockerfile
FROM node:20-alpine AS base

# Install dumb-init and wget (for healthcheck)
RUN apk add --no-cache dumb-init wget

WORKDIR /app

# ---- Dependencies ----
FROM base AS deps
COPY package*.json ./
RUN npm ci --omit=dev

# ---- Production ----
FROM base AS runner
ENV NODE_ENV=production

# Create non-root user
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 expressuser

# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Set ownership
RUN chown -R expressuser:nodejs /app

USER expressuser

EXPOSE 4000

# Use dumb-init to handle signals
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "index.js"]
