FROM node:24-alpine

WORKDIR /app

ARG NPM_REGISTRY=https://registry.npmjs.org
ARG NEXT_PUBLIC_API_URL=http://localhost:3000
ARG NEXT_PUBLIC_WS_URL=ws://localhost:8085/ws
ARG INTERNAL_API_URL=http://api-gateway:3000
ARG INTERNAL_WS_URL=http://media-ws:8085

COPY package.json package-lock.json .npmrc ./
COPY apps/sso-core/package.json ./apps/sso-core/
COPY apps/api-gateway/package.json ./apps/api-gateway/
COPY apps/frontend/package.json ./apps/frontend/
COPY apps/docs/package.json ./apps/docs/

RUN --mount=type=cache,target=/root/.npm \
  npm config set registry "${NPM_REGISTRY}" && \
  for i in 1 2 3 4 5; do \
    echo "npm ci: попытка $i/5" && \
    npm ci --no-audit --no-fund && exit 0; \
    echo "npm ci: ошибка сети, повтор через $((i * 15)) сек..."; \
    sleep $((i * 15)); \
  done; \
  echo "npm ci: все попытки исчерпаны"; \
  exit 1

COPY apps ./apps
COPY shared ./shared
COPY tsconfig.base.json ./

ENV NEXT_TELEMETRY_DISABLED="1"
ENV PORT="3000"
ENV HOSTNAME="0.0.0.0"
ENV NEXT_PUBLIC_API_URL="${NEXT_PUBLIC_API_URL}"
ENV NEXT_PUBLIC_WS_URL="${NEXT_PUBLIC_WS_URL}"
ENV INTERNAL_API_URL="${INTERNAL_API_URL}"
ENV INTERNAL_WS_URL="${INTERNAL_WS_URL}"

RUN npm --workspace @lendry/frontend run build

EXPOSE 3000

CMD ["npm", "--workspace", "@lendry/frontend", "run", "start"]
