# Stage 1: Build the client
FROM node:20-alpine AS client-build
WORKDIR /app/client
COPY client/package*.json ./
RUN npm install
COPY client/ ./
RUN npm run build

# Stage 2: Setup server and production image
FROM node:20-alpine
WORKDIR /app

RUN apk add --no-cache python3 make g++

# Copy server code
COPY server/package*.json ./server/
WORKDIR /app/server
RUN npm install --production
COPY server/ ./

# Copy built client
COPY --from=client-build /app/client/dist /app/client/dist

# Expose port (using 3001 as that's what the server defaults to)
EXPOSE 3001

# The server listens on 3001 by default, or PORT env
ENV PORT=3001
ENV NODE_ENV=production

CMD ["node", "src/index.js"]