# ---- Stage 1: build the Vite app ----
FROM node:22-alpine AS build

WORKDIR /app

# Install dependencies from the lockfile first so this layer is cached
# as long as package.json / package-lock.json are unchanged.
COPY package.json package-lock.json ./
RUN npm ci

# Build the static bundle into /app/dist
COPY . .
RUN npm run build

# ---- Stage 2: serve with Nginx ----
FROM nginx:alpine

# Drop Nginx's default "Welcome" page
RUN rm -rf /usr/share/nginx/html/*

# Vite outputs to "dist", not "build"
COPY --from=build /app/dist /usr/share/nginx/html

# Files under public/ keep their source permissions through the build, so
# normalize them: anything not world-readable is a 403 from the nginx worker.
RUN chmod -R a+rX /usr/share/nginx/html

COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80 3000

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