# syntax=docker/dockerfile:1

###############################################
# 🧱 Base Stage (Install Dependencies Once)
###############################################
FROM node:22-alpine AS base

WORKDIR /usr/src/app

COPY package*.json ./
COPY scripts ./scripts
RUN npm ci --legacy-peer-deps --force

RUN npm install -g @angular/cli@^20.0.0

COPY . .

RUN sed -i 's/\r//' /usr/src/app/scripts/dockerbuild-mfe.sh && chmod +x /usr/src/app/scripts/dockerbuild-mfe.sh

# 2GB per Node process — with 2 parallel builds peak usage is ~4-6GB (safe within 12GB WSL2)
ENV NODE_OPTIONS=--max-old-space-size=4096

###############################################
# 🧩 Build HOST  (independent, tiny)
###############################################
FROM base AS build-host

RUN ./scripts/dockerbuild-mfe.sh "HOST" gbhost

###############################################
# 🧩 PHASE 1 — g1 + g2 run in parallel
###############################################

# Group 1: admin, analytics, costing, crmsales, crmservice, facility
FROM base AS build-g1

RUN ./scripts/dockerbuild-mfe.sh "GROUP 1" admin analytics costing crmsales crmservice facility && \
    touch /tmp/.g1-done

# Group 2: finance, framework, hrms, inventory, logistics, maintenance
FROM base AS build-g2

RUN ./scripts/dockerbuild-mfe.sh "GROUP 2" finance framework hrms inventory logistics maintenance && \
    touch /tmp/.g2-done

###############################################
# 🧩 PHASE 2 — g3 + g4 run in parallel
#    (both wait for g1 AND g2 to finish)
###############################################

# Group 3: projects, purchase, quality, security sales
FROM base AS build-g3

COPY --from=build-g1 /tmp/.g1-done /tmp/.g1-done
COPY --from=build-g2 /tmp/.g2-done /tmp/.g2-done

RUN ./scripts/dockerbuild-mfe.sh "GROUP 3" projects purchase quality sales security && \
    touch /tmp/.g3-done

# Group 4: subcontract, wms, attendanceandpayroll, ess, planning
FROM base AS build-g4

COPY --from=build-g1 /tmp/.g1-done /tmp/.g1-done
COPY --from=build-g2 /tmp/.g2-done /tmp/.g2-done

RUN ./scripts/dockerbuild-mfe.sh "GROUP 4" subcontract wms attendanceandpayroll ess planning && \
    touch /tmp/.g4-done

###############################################
# 🧩 PHASE 3 — g5 + g6 run in parallel
#    (both wait for g3 AND g4 to finish)
###############################################

# Group 5: fam, gbservicedesk, dashboard, dms, cms
FROM base AS build-g5

COPY --from=build-g3 /tmp/.g3-done /tmp/.g3-done
COPY --from=build-g4 /tmp/.g4-done /tmp/.g4-done

RUN ./scripts/dockerbuild-mfe.sh "GROUP 5" fam gbservicedesk dashboard dms

# Group 6: expensesandclaim, foundry, gbgateway, gbstudio, recruitment
FROM base AS build-g6

COPY --from=build-g3 /tmp/.g3-done /tmp/.g3-done
COPY --from=build-g4 /tmp/.g4-done /tmp/.g4-done

RUN ./scripts/dockerbuild-mfe.sh "GROUP 6" expensesandclaim foundry gbgateway gbstudio recruitment

###############################################
# 🌐 Final NGINX Image
###############################################
FROM fholzer/nginx-brotli:latest

RUN apk add --no-cache gettext

RUN rm -rf /usr/share/nginx/html/*

COPY --from=build-host /usr/src/app/dist/gbhost/browser /usr/share/nginx/html
COPY projects/gbhost/public/config /usr/share/nginx/html/config

COPY --from=build-g1 /usr/src/app/dist /tmp/dist-g1
COPY --from=build-g2 /usr/src/app/dist /tmp/dist-g2
COPY --from=build-g3 /usr/src/app/dist /tmp/dist-g3
COPY --from=build-g4 /usr/src/app/dist /tmp/dist-g4
COPY --from=build-g5 /usr/src/app/dist /tmp/dist-g5
COPY --from=build-g6 /usr/src/app/dist /tmp/dist-g6

RUN mkdir -p /usr/share/nginx/html/modules && \
    cp -r /tmp/dist-g1/* /usr/share/nginx/html/modules/ && \
    cp -r /tmp/dist-g2/* /usr/share/nginx/html/modules/ && \
    cp -r /tmp/dist-g3/* /usr/share/nginx/html/modules/ && \
    cp -r /tmp/dist-g4/* /usr/share/nginx/html/modules/ && \
    cp -r /tmp/dist-g5/* /usr/share/nginx/html/modules/ && \
    cp -r /tmp/dist-g6/* /usr/share/nginx/html/modules/ && \
    rm -rf /tmp/dist-*

COPY nginx.conf.template /etc/nginx/templates/default.conf.template

COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN sed -i 's/\r//' /docker-entrypoint.sh && chmod +x /docker-entrypoint.sh

EXPOSE 80
ENTRYPOINT ["/docker-entrypoint.sh"]
