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

# Stage 1: Bygg applikationen
FROM node:22-alpine AS builder
WORKDIR /app

# Kopiera backend-filer
COPY package.json ./
COPY prisma ./prisma
COPY src ./src
COPY tsconfig.json ./
COPY nest-cli.json ./

# Köra npm install
RUN npm install

RUN npx prisma generate
RUN npm test
RUN npm run build

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

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

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