# Development stage
FROM node:18-alpine as development

WORKDIR /app

# 1. Copy package files and install dependencies
COPY package*.json ./
RUN npm install

# 2. Copy all source files (including the /public and /src directories) 
COPY . .

# 3. Run the build script to generate the production-ready assets
#    This step now runs *after* the source code is copied.
RUN npm run build

# NOTE: The CMD for the development stage is only used if you target this stage
CMD ["npm", "start"]

# Production stage
FROM nginx:alpine as production

# 4. Copy the static build artifacts from the 'development' stage
COPY --from=development /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf

RUN mkdir -p /var/cache/nginx/client_temp && \
    chown -R 1000:1000 /var/cache/nginx

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]