# ============================================================
# INSIGHT CRM-Service - Multi-Stage Dockerfile
# ============================================================

# --- Base Stage ---
FROM node:20-alpine AS base
WORKDIR /app
RUN apk add --no-cache openssl

# --- Dependencies Stage ---
FROM base AS deps
COPY package.json package-lock.json* ./
RUN npm ci --ignore-scripts
# Prisma Generate braucht die Schema-Dateien
COPY prisma ./prisma
RUN npx prisma generate --schema=prisma/crm.schema.prisma

# --- Development Stage ---
FROM base AS development
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npx prisma generate --schema=prisma/crm.schema.prisma
EXPOSE 3100
CMD ["npm", "run", "start:dev"]

# --- Build Stage ---
FROM base AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

# --- Production Stage ---
FROM base AS production
WORKDIR /app
ENV NODE_ENV=production

# Nur Produktions-Dependencies
COPY package.json package-lock.json* ./
RUN npm ci --omit=dev --ignore-scripts

# Prisma Client generieren
COPY prisma ./prisma
RUN npx prisma generate --schema=prisma/crm.schema.prisma

# Kompilierter Code
COPY --from=build /app/dist ./dist

# Non-root User
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nestjs -u 1001 -G nodejs
USER nestjs

EXPOSE 3100
CMD ["node", "dist/main"]
