# Multi-stage build with memory optimization
FROM ci.nxtgauge.com/admin/node:20-slim AS builder
WORKDIR /app

# Skip browser downloads
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
ENV CYPRESS_INSTALL_BINARY=0
# NODE_ENV=production must NOT be set in this stage: with it set, `npm ci`
# skips devDependencies (vite, @tailwindcss/vite, etc.) that `npm run build`
# itself needs (SolidStart's build runs through vite) - the build then fails
# with ERR_MODULE_NOT_FOUND for 'vite'. It belongs in the runtime stage only,
# where it actually affects the running server, not here.

# GATEWAY_URL is resolved at runtime from the process environment (see
# src/lib/server/gateway.ts), not baked in at build time.

# Install build dependencies
RUN apt-get update && apt-get install -y python3 make g++ git && rm -rf /var/lib/apt/lists/*

# Copy package files
COPY package*.json ./

# Install dependencies (--include=dev is redundant with NODE_ENV unset, but
# explicit so this doesn't silently break again if NODE_ENV=production ever
# gets reintroduced above)
RUN npm ci --legacy-peer-deps --prefer-offline --no-audit --include=dev

# Copy source
COPY . .

# Build with memory optimization
ENV NODE_OPTIONS="--max-old-space-size=4096"
RUN npm run build

# Runtime stage
FROM ci.nxtgauge.com/admin/node:20-alpine
WORKDIR /app

# Copy built output
COPY --from=builder /app/.output ./.output

ENV NODE_ENV=production
ENV PORT=9202
ENV HOST=0.0.0.0
EXPOSE 9102

CMD ["node", ".output/server/index.mjs"]
