# Stage 1: Build the application
FROM node:20 AS build

# Set working directory
WORKDIR /app

# Copy package.json and package-lock.json for caching npm install
COPY package*.json ./

# Install dependencies (use --frozen-lockfile for npm 7+)
RUN npm install 

# Copy the rest of the app
COPY . .

# Build the NestJS application
RUN npm run build

# Stage 2: Production environment
FROM node:20-slim AS production

# Set working directory
WORKDIR /app

# Copy only necessary files from the build stage
COPY --from=build /app/package*.json ./
COPY --from=build /app/dist ./dist

# Install production dependencies only
RUN npm ci --only=production

# Expose the app port (default for NestJS)
EXPOSE 3000

# Set NODE_ENV to production
ENV NODE_ENV=production

# Run the application
CMD ["node", "dist/main"]

