# ---------- Build stage ----------
FROM node:20-alpine AS builder

ARG TENANT=tis
ARG DIST_DIR=tis

WORKDIR /app

RUN npm config set registry "https://hub.megan.ir/npm/"
RUN npm install -g pnpm

COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile

COPY . .

RUN if [ "$TENANT" = "default" ]; then \
      pnpm run build; \
    else \
      pnpm run build:$TENANT; \
    fi


# ---------- Runtime stage ----------
FROM nginx:alpine

# Remove default config
RUN rm -rf /etc/nginx/conf.d/*

# Copy optimized config
COPY nginx.conf /etc/nginx/conf.d/default.conf

ARG DIST_DIR=tis

COPY --from=builder /app/dist/${DIST_DIR}/browser /usr/share/nginx/html

# Optional: reduce image size
# RUN apk add --no-cache brotli

EXPOSE 8090

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