# 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/scripts scripts/
COPY --from=builder /app/certs certs/
COPY package.json .

RUN chmod +x /app/scripts/docker-entrypoint.sh

# Expose HTTPS port
EXPOSE 3000

ENTRYPOINT ["/app/scripts/docker-entrypoint.sh"]
