# Build stages run on the native build platform (QEMU-free). The console build
# emits static assets, so pnpm/vite must not execute under arm64 emulation where
# QEMU aborts with SIGILL. Only the final nginx stage targets the requested arch.
FROM --platform=$BUILDPLATFORM node:26-alpine AS base
RUN apk add --no-cache curl bash
COPY package.json /tmp/pm/package.json
RUN npm install -g corepack && corepack enable \
  && corepack prepare "$(node -p "require('/tmp/pm/package.json').packageManager")" --activate
ENV PNPM_HOME=/root/.local/share/pnpm
ENV PATH=$PNPM_HOME:$PATH

# Build stage
FROM base AS builder

WORKDIR /app

# Copy workspace config first for dependency resolution
COPY pnpm-workspace.yaml .npmrc package.json pnpm-lock.yaml ./
COPY console/package.json ./console/
COPY core-api/package.json ./core-api/

RUN pnpm install --frozen-lockfile

# Copy full source
COPY console/ ./console

ENV NODE_ENV production
ENV VITE_API_URL=APP_VITE_API_URL

RUN pnpm --filter console run build && \
  rm -rf console/node_modules/.cache

# Nginx runtime stage
FROM nginx:stable-alpine

COPY --from=builder /app/console/dist /usr/share/nginx/html
COPY console/nginx.conf /etc/nginx/conf.d/default.conf

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