# Byggas från backend-mappen: docker build -t recipe-api:local .

# Stage 1: Installera enbart produktionsberoenden (exkl. devDeps)
FROM node:24.15.0-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
COPY prisma ./prisma
RUN npm ci --omit=dev
RUN ./node_modules/.bin/prisma generate

# Stage 2: Bygg applikationen (alla beroenden, kör tester)
FROM node:24.15.0-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY prisma ./prisma
COPY src ./src
COPY tsconfig.json nest-cli.json ./
RUN ./node_modules/.bin/prisma generate
RUN npm test
RUN npm run build

# Stage 3: Kör applikationen
FROM node:24.15.0-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production

COPY package.json ./
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/prisma ./prisma
COPY --from=builder /app/dist ./dist

EXPOSE 8080
CMD ["sh", "-c", "until ./node_modules/.bin/prisma migrate deploy; do echo 'Migration failed, retrying in 5s...'; sleep 5; done && node dist/main"]
