FROM node:20-alpine AS base
# Prisma migration engine requires OpenSSL
RUN apk add --no-cache openssl
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

FROM base AS dev
RUN npm ci
COPY . .
CMD ["npm", "run", "dev"]

FROM base AS prod
# Install prisma CLI for migrate deploy at runtime
RUN npm install prisma --save-dev
COPY . .
# Generate Prisma Client
RUN npx prisma generate
# Fix ownership so the node user can write engine cache
RUN chown -R node:node /app
USER node
CMD ["/bin/sh", "/app/docker-entrypoint.sh"]
