# CaseGambling — multi-stage production image
# Docker Hub: foufure/casegambling (repository names must be lowercase)

FROM node:20-alpine AS client-build
WORKDIR /app/client
COPY client/package.json client/package-lock.json ./
RUN npm ci
COPY client/ ./
RUN npm run build

FROM node:20-alpine AS server-deps
WORKDIR /app
RUN apk add --no-cache python3 make g++
COPY server/package.json server/package-lock.json ./
RUN npm ci
COPY server/prisma ./prisma
RUN npx prisma generate

FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3001

RUN apk add --no-cache openssl libc6-compat wget \
  && mkdir -p /data /app/uploads

COPY --from=server-deps /app/node_modules ./node_modules
COPY --from=server-deps /app/package.json ./
COPY server/ ./
COPY --from=client-build /app/client/dist ./public

EXPOSE 3001

VOLUME ["/data", "/app/uploads"]

# --accept-data-loss: needed for safe widenings (e.g. Int → BigInt on money columns).
# Without it, prisma db push refuses to apply and the container restart-loops.
CMD ["sh", "-c", "npx prisma db push --accept-data-loss && node src/index.js"]
