FROM node:20-bookworm AS base

FROM base AS builder
ARG APP_DIR=/opt/bridge-whatsapp
RUN mkdir -p ${APP_DIR}/
RUN npm i -g turbo
WORKDIR ${APP_DIR}
COPY . .
RUN turbo prune --scope=@link-stack/bridge-whatsapp --docker

FROM base AS installer
ARG APP_DIR=/opt/bridge-whatsapp
WORKDIR ${APP_DIR}
COPY --from=builder ${APP_DIR}/out/json/ .
COPY --from=builder ${APP_DIR}/out/full/ .
COPY --from=builder ${APP_DIR}/out/package-lock.json ./package-lock.json
RUN npm ci
RUN npm i -g turbo
RUN turbo run build --filter=@link-stack/bridge-whatsapp

FROM base as runner
ARG BUILD_DATE
ARG VERSION
ARG APP_DIR=/opt/bridge-whatsapp
RUN mkdir -p ${APP_DIR}/
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
    apt-get install -y --no-install-recommends \
    dumb-init
WORKDIR ${APP_DIR}
COPY --from=installer ${APP_DIR} ./
RUN chown -R node:node ${APP_DIR}
WORKDIR ${APP_DIR}/apps/bridge-whatsapp/
RUN chmod +x docker-entrypoint.sh
USER node
RUN mkdir /home/node/baileys
EXPOSE 5000
ENV PORT 5000
ENV NODE_ENV production
ENTRYPOINT ["/opt/bridge-whatsapp/apps/bridge-whatsapp/docker-entrypoint.sh"]
