# The three Vite apps, built and served by one Nginx.
#
# All three depend on the @mirwal/shared workspace package, resolved by npm
# workspaces as a symlink under node_modules — there is no way to build one
# app in isolation without an `npm ci` that sees the whole workspace graph.
# This copies the full tree before installing rather than the usual
# copy-package-json-first trick, trading layer-cache efficiency (any source
# change reinstalls) for not having to keep a hand-maintained list of every
# workspace's package.json in sync with the real one.
#
# Build with, e.g.:
#   docker build -f web/Dockerfile \
#     --build-arg VITE_API_BASE_URL=https://api.mirwal.pk \
#     --build-arg VITE_SITE_URL=https://mirwal.pk \
#     --build-arg VITE_STOREFRONT_URL=https://mirwal.pk \
#     --build-arg VITE_SELLER_URL=https://seller.mirwal.pk \
#     --build-arg VITE_ADMIN_URL=https://admin.mirwal.pk \
#     -t mirwal-web .
# docker-compose.yml passes these from the root .env for you.

FROM node:24-alpine AS build
WORKDIR /app
COPY . .

# VITE_* values are inlined into the bundle at build time — there is no way
# to change them after this step short of rebuilding the image.
ARG VITE_API_BASE_URL
ARG VITE_SITE_URL
ARG VITE_STOREFRONT_URL
ARG VITE_SELLER_URL
ARG VITE_ADMIN_URL
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL \
    VITE_SITE_URL=$VITE_SITE_URL \
    VITE_STOREFRONT_URL=$VITE_STOREFRONT_URL \
    VITE_SELLER_URL=$VITE_SELLER_URL \
    VITE_ADMIN_URL=$VITE_ADMIN_URL

RUN npm ci \
 && npm run build:storefront \
 && npm run build:seller \
 && npm run build:admin

FROM nginx:1.27-alpine
COPY --from=build /app/apps/storefront/dist /usr/share/nginx/html/storefront
COPY --from=build /app/apps/seller/dist /usr/share/nginx/html/seller
COPY --from=build /app/apps/admin/dist /usr/share/nginx/html/admin
# The stock default.conf would otherwise win the "no server_name matched"
# case by alphabetical load order — _catchall.conf in web/nginx/ replaces it
# with an explicit, deliberate one.
RUN rm -f /etc/nginx/conf.d/default.conf
COPY web/nginx/*.conf /etc/nginx/conf.d/
EXPOSE 80
