# Multi-stage build - funciona para dev e prod
ARG NODE_ENV=production

# Build stage
FROM node:lts-alpine3.22 AS builder

ARG NODE_ENV

WORKDIR /app

COPY package*.json ./

# Build sempre precisa dos @types e ferramentas de compilação
RUN npm ci --include=dev

COPY . .

RUN npm run build

# Runtime stage
FROM node:lts-alpine3.22

ARG NODE_ENV

WORKDIR /app

# Copiar arquivos do builder
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/prisma ./prisma
COPY --from=builder /app/package*.json ./

# Instalar apenas dependências de produção no runtime
RUN if [ "$NODE_ENV" = "production" ]; then \
  npm ci --only=production; \
  else \
  npm ci; \
  fi

ARG PORT=4000
ENV NODE_ENV=${NODE_ENV}
ENV PORT=${PORT}

EXPOSE ${PORT}

CMD ["npm", "start"]