# Stage 1: dipendenze condivise (build + dev)
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci

# Stage 2: dev — hot reload via Vite, sorgente montato da bind-mount
FROM deps AS dev
ENV NODE_ENV=development
EXPOSE 5173
CMD ["npm", "run", "dev"]

# Stage 3: build frontend
FROM deps AS builder
COPY . .
RUN npm run build

# Stage 4: runtime di produzione
FROM node:20-alpine AS runtime
WORKDIR /app
ENV NODE_ENV=production PORT=3000

COPY package*.json ./
RUN npm ci --omit=dev

COPY server.js ./
COPY src/gameState.js src/websocket-handler.js src/server-utils.js src/persist.js src/spot-utils.js ./src/
COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD ["node", "server.js"]
