FROM node:20-alpine AS builder

WORKDIR /app

COPY package.json package-lock.json* ./
RUN NODE_ENV=development npm install --legacy-peer-deps

COPY . .
RUN npm run build

FROM node:20-alpine AS runner

RUN apk add --no-cache p7zip

WORKDIR /app

COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./
COPY --from=builder /app/migrations ./migrations
COPY --from=builder /app/drizzle.config.ts ./drizzle.config.ts
COPY --from=builder /app/shared ./shared
COPY --from=builder /app/tsconfig.json ./tsconfig.json

ENV NODE_ENV=production
ENV PORT=5000

EXPOSE 5000

HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
  CMD wget -qO- http://127.0.0.1:5000/ || exit 1

CMD ["node", "dist/index.cjs"]
