# Stage 1: Build Frontend
FROM node:18-alpine AS frontend-build
WORKDIR /app/frontend
COPY frontend/package*.json ./
RUN npm install
COPY frontend/ ./
RUN npm run build

# Stage 2: Build Backend & Serve
FROM node:18-alpine
WORKDIR /app/backend

# Install backend dependencies
COPY backend/package*.json ./
RUN npm install --production

# Copy backend source
COPY backend/ ./

# Copy built frontend to the public directory expected by server.js
# (__dirname is /app/backend/src, so ../../public is /app/public)
COPY --from=frontend-build /app/frontend/dist /app/public

# Set production environment
ENV NODE_ENV=production
ENV PORT=3010

EXPOSE 3010

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