# syntax=docker/dockerfile:1.4

# Use Alpine - no apt-get, much faster!
FROM node:18-alpine AS development

WORKDIR /app

# Install build tools with apk (seconds vs minutes)
RUN apk add --no-cache python3 make g++

# Copy package files first for better layer caching
COPY package.json package-lock.json* ./

# Install dependencies 
RUN npm ci --legacy-peer-deps --no-optional

# Copy source code
COPY . .

ENV CI=true
ENV PORT=3000

CMD ["npm", "start"]

# Build stage
FROM development AS build
RUN npm run build

# Production stage
FROM nginx:alpine AS production

# Copy nginx config if it exists, otherwise use default
COPY --from=build /app/.nginx/nginx.conf /etc/nginx/conf.d/default.conf 2>/dev/null || echo "Using default nginx config"

# Copy built app
COPY --from=build /app/build /usr/share/nginx/html

ENTRYPOINT ["nginx", "-g", "daemon off;"]