FROM node:24-alpine

WORKDIR /app

ARG NPM_REGISTRY=https://registry.npmjs.org

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 PORT="3000"
ENV SSO_CORE_GRPC_URL="sso-core:50051"
ENV JWT_ACCESS_SECRET="docker-access-secret"

RUN npm --workspace @lendry/api-gateway run build

EXPOSE 3000

CMD ["node", "apps/api-gateway/dist/main.js"]
