# Use the official Node.js image.
FROM docker.si24.ir/node:20-alpine AS builder

# Create and change to the app directory.
WORKDIR /app

# Copy application dependency manifests to the container image.
COPY package*.json ./

# Specify the non-root user and set ownership (with verbose mode)
RUN adduser -D ittalie
RUN chown -Rv ittalie:ittalie /app

# Switch to the non-root user
USER ittalie

# Install dependencies without running scripts
RUN npm config set registry https://repo.si24.ir/repository/npm/
RUN npm ci --ignore-scripts -d

# Copy only the necessary source code
COPY src/ ./src/
COPY tsconfig.json ./
COPY nest-cli.json ./
COPY public ./public/

# Build the NestJS application.
RUN npm run build

# --- Production Stage ---
FROM docker.si24.ir/node:22-alpine

# Create and change to the app directory.
WORKDIR /app

# Copy package.json and package-lock.json for production dependencies.
COPY package*.json ./

# Specify the non-root user and set ownership (with verbose mode)
RUN adduser -D ittalie
RUN chown -Rv ittalie:ittalie /app

# Switch to the non-root user
USER ittalie

# Install dependencies without running scripts
RUN npm config set registry https://repo.si24.ir/repository/npm/
RUN npm ci --ignore-scripts -d


# Copy the built application from the builder stage.
COPY --from=builder /app/dist ./dist

# Run the web service on container startup.
CMD ["npm", "run", "start:prod"]
