FROM node:22-alpine AS builder
WORKDIR /app

ARG HTTP_PROXY=""
ARG HTTPS_PROXY=""
ARG NO_PROXY="localhost,127.0.0.1"
ENV HTTP_PROXY=${HTTP_PROXY}
ENV HTTPS_PROXY=${HTTPS_PROXY}
ENV NO_PROXY=${NO_PROXY}
ENV NODE_TLS_REJECT_UNAUTHORIZED=0
ENV NPM_CONFIG_STRICT_SSL=false
ENV HUSKY=0

RUN npm config set strict-ssl false \
  && npm install -g pnpm@10.6.5

COPY package.json pnpm-workspace.yaml pnpm-lock.yaml tsconfig.base.json ./
COPY packages/ packages/
COPY dashboards/tracker-web/package.json dashboards/tracker-web/

RUN pnpm install --ignore-scripts

COPY dashboards/tracker-web/ dashboards/tracker-web/

ENV NEXT_TELEMETRY_DISABLED=1

RUN pnpm -r --filter @bytelyst/tracker-web... build
RUN pnpm --filter @bytelyst/tracker-web deploy --legacy --ignore-scripts /app/deploy

FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3003
ENV HOSTNAME=0.0.0.0
ENV HUSKY=0

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder --chown=nextjs:nodejs /app/dashboards/tracker-web/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/dashboards/tracker-web/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/dashboards/tracker-web/public ./public

USER nextjs

EXPOSE 3003

CMD ["node", "dashboards/tracker-web/server.js"]
