# Build stage
FROM node:22-alpine AS builder
WORKDIR /app

# Copy package files
COPY package.json ./
COPY package-lock.json ./
RUN npm ci

# Copy source and build
COPY . .
ENV NODE_ENV=production
RUN npm run build || exit 1

# Production stage
FROM node:22-alpine AS runtime
WORKDIR /app

# Copy built assets and dependencies
COPY --from=builder /app/build ./build
COPY --from=builder /app/node_modules ./node_modules
COPY package.json .

# Non-root user
RUN addgroup -S nsct && adduser -S nsct -G nsct
USER nsct

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 CMD wget -qO- http://localhost:3000/health || exit 1

EXPOSE 3000
ENV HOST=0.0.0.0
ENV PORT=3000

CMD ["node", "build"]