# Build context: learning_ai_invt_trdg/ (monorepo root)
# --- Stage 1: Build ---
FROM node:20-alpine AS builder

RUN corepack enable && corepack prepare pnpm@10.6.5 --activate

WORKDIR /app

ARG GITEA_NPM_TOKEN
ARG BYTELYST_PACKAGE_SOURCE=vendor
ENV GITEA_NPM_TOKEN=${GITEA_NPM_TOKEN}
ENV BYTELYST_PACKAGE_SOURCE=${BYTELYST_PACKAGE_SOURCE}

COPY .npmrc .pnpmfile.cjs pnpm-workspace.yaml pnpm-lock.yaml* ./
COPY package.json ./package.json
COPY web/package.json ./web/package.json
COPY vendor/ ./vendor/

RUN pnpm install --filter @bytelyst/trading-web

COPY web/ ./web/
COPY shared/ ./shared/

# Build-time env vars (baked into the static bundle)
ARG VITE_PRODUCT_ID=invttrdg
ARG VITE_PLATFORM_URL=https://api.bytelyst.com/platform/api
ARG VITE_TRADING_API_URL=https://api.bytelyst.com/invttrdg

ENV VITE_PRODUCT_ID=${VITE_PRODUCT_ID}
ENV VITE_PLATFORM_URL=${VITE_PLATFORM_URL}
ENV VITE_TRADING_API_URL=${VITE_TRADING_API_URL}

WORKDIR /app/web
RUN pnpm run build

# --- Stage 2: Serve ---
FROM nginx:alpine

COPY --from=builder /app/web/dist /usr/share/nginx/html

# SPA fallback: all routes serve index.html
RUN printf 'server {\n  listen 3085;\n  root /usr/share/nginx/html;\n  index index.html;\n  location / {\n    try_files $uri $uri/ /index.html;\n  }\n  gzip on;\n  gzip_types text/plain text/css application/javascript application/json;\n}\n' > /etc/nginx/conf.d/default.conf

EXPOSE 3085
CMD ["nginx", "-g", "daemon off;"]
