# Stage 1: Build
FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies (force reinstall for Alpine Linux compatibility)
RUN rm -rf node_modules package-lock.json && \
    npm cache clean --force && \
    npm install

# Copy source code
COPY . .

# Build the application
RUN npm run build
RUN npm prune --production

# Stage 2: Run
FROM node:20-alpine

WORKDIR /app

# Copy built application and dependencies
COPY --from=builder /app/build build/
COPY --from=builder /app/node_modules node_modules/
COPY --from=builder /app/server.js .
COPY --from=builder /app/certs certs/
COPY package.json .

# Expose HTTPS port
EXPOSE 3000

# Start the application with HTTPS
CMD ["node", "server.js"]
