FROM node:22.23.4-bookworm-slim@sha256:aaa4b5baf1ecff3b283486bb1f90405cdda225a3e6f79660d2c559fa510a7292

ENV NEXT_TELEMETRY_DISABLED=1

RUN apt-get update && apt-get install -y --no-install-recommends \
    g++ make python3-pip bash nginx openssl ca-certificates \
    && rm -rf /var/lib/apt/lists/* \
    && addgroup --system www \
    && adduser --system --ingroup www --home /www --shell /usr/sbin/nologin www \
    && mkdir -p /www \
    && chown -R www:www /www /var/lib/nginx

RUN npm --no-update-notifier --no-fund --global install pnpm@10.6.1 pm2@7.1.2

WORKDIR /app
COPY . /app
COPY var/docker/nginx.conf /etc/nginx/nginx.conf

ARG NEXT_PUBLIC_HITPOST_SOURCE_REVISION
LABEL org.opencontainers.image.revision=${NEXT_PUBLIC_HITPOST_SOURCE_REVISION} \
      org.opencontainers.image.licenses=AGPL-3.0-only
RUN test -s "apps/frontend/public/hitpost-source-${NEXT_PUBLIC_HITPOST_SOURCE_REVISION}.tar.gz" \
    && test -s LICENSE \
    && test -s libraries/nestjs-libraries/src/database/prisma/migrations/0_init/migration.sql
RUN pnpm install --frozen-lockfile && test ! -e node_modules/polotno
RUN test -z "$(find . '(' -path '*/node_modules/@mastra' -o -path '*/node_modules/mastra' -o -path '*/node_modules/@ag-ui/mastra' ')' -print -quit)"
RUN NODE_OPTIONS="--max-old-space-size=4096" STORAGE_PROVIDER=local \
    NEXT_PUBLIC_VERSION="hitpost-$(printf '%.12s' "$NEXT_PUBLIC_HITPOST_SOURCE_REVISION")" \
    pnpm run build

CMD ["/app/scripts/growth-os/release/start.sh"]
