FROM node:20-alpine AS builder
WORKDIR /app

# Install service dependencies
COPY services/referral-service/package*.json ./services/referral-service/
WORKDIR /app/services/referral-service
RUN npm install

# Copy service source and build
COPY services/referral-service/ ./
RUN npm run build

FROM node:20-alpine
WORKDIR /app
RUN apk add --no-cache dumb-init
COPY --from=builder /app/services/referral-service/dist ./dist
COPY --from=builder /app/services/referral-service/node_modules ./node_modules
COPY --from=builder /app/services/referral-service/package.json ./
USER node
EXPOSE 3013
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:3013/api/v1/health || exit 1
CMD ["dumb-init", "node", "dist/main"]
