FROM node:20-alpine AS builder

WORKDIR /app

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

# Copy apps/website, packages, and config for building
COPY apps/website apps/website/
COPY packages core/
COPY apps/website/tsconfig.json apps/website/
COPY scripts scripts/
COPY tsconfig.base.json ./


# Build the Next.js application
# Run from the root workspace context
RUN node ./node_modules/next/dist/bin/next build


# Production stage: slim image with only production dependencies and built files
FROM node:20-alpine AS production_final

WORKDIR /app

# Install wget for healthchecks
RUN apk add --no-cache wget


# Copy package files and install production dependencies only
COPY --from=builder /app/package.json ./
COPY --from=builder /app/package-lock.json ./
RUN npm ci --omit=dev

# Copy built Next.js application
COPY --from=builder /app/apps/website/.next ./apps/website/.next
COPY --from=builder /app/apps/website/public ./apps/website/public
COPY --from=builder /app/apps/website/package.json ./apps/website/package.json
COPY --from=builder /app/apps/website/next.config.mjs ./apps/website/next.config.mjs

# Copy packages (needed for runtime dependencies)
COPY --from=builder /app/packages ./packages

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Run Next.js in production mode from the root workspace context
CMD ["node", "./node_modules/next/dist/bin/next", "start"]