# Build context: repo root (docker compose sets context: .)
FROM node:22-alpine AS builder
RUN npm install -g pnpm@10
WORKDIR /app

# Copy workspace config + lockfile for dependency resolution
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml tsconfig.base.json ./

# Copy all workspace package.json files (pnpm needs these for --frozen-lockfile resolution)
COPY packages/api-client/package.json packages/api-client/
COPY packages/auth/package.json packages/auth/
COPY packages/auth-client/package.json packages/auth-client/
COPY packages/blob/package.json packages/blob/
COPY packages/blob-client/package.json packages/blob-client/
COPY packages/broadcast-client/package.json packages/broadcast-client/
COPY packages/config/package.json packages/config/
COPY packages/cosmos/package.json packages/cosmos/
COPY packages/dashboard-components/package.json packages/dashboard-components/
COPY packages/datastore/package.json packages/datastore/
COPY packages/design-tokens/package.json packages/design-tokens/
COPY packages/diagnostics-client/package.json packages/diagnostics-client/
COPY packages/errors/package.json packages/errors/
COPY packages/events/package.json packages/events/
COPY packages/extraction/package.json packages/extraction/
COPY packages/fastify-core/package.json packages/fastify-core/
COPY packages/feature-flag-client/package.json packages/feature-flag-client/
COPY packages/feedback-client/package.json packages/feedback-client/
COPY packages/kill-switch-client/package.json packages/kill-switch-client/
COPY packages/llm/package.json packages/llm/
COPY packages/logger/package.json packages/logger/
COPY packages/monitoring/package.json packages/monitoring/
COPY packages/offline-queue/package.json packages/offline-queue/
COPY packages/platform-client/package.json packages/platform-client/
COPY packages/push/package.json packages/push/
COPY packages/react-auth/package.json packages/react-auth/
COPY packages/react-native-platform-sdk/package.json packages/react-native-platform-sdk/
COPY packages/speech/package.json packages/speech/
COPY packages/storage/package.json packages/storage/
COPY packages/survey-client/package.json packages/survey-client/
COPY packages/sync/package.json packages/sync/
COPY packages/telemetry-client/package.json packages/telemetry-client/
COPY packages/testing/package.json packages/testing/
COPY services/mcp-server/package.json services/mcp-server/

# Install all workspace deps
RUN pnpm install --frozen-lockfile

# Copy source
COPY packages/ packages/
COPY services/mcp-server/tsconfig.json services/mcp-server/
COPY services/mcp-server/src/ services/mcp-server/src/

# Build packages first, then service
RUN pnpm -r --filter @bytelyst/mcp-server... build

# Deploy to isolated directory (production deps only)
RUN pnpm --filter @bytelyst/mcp-server deploy --legacy /app/deploy

# ── Production ─────────────────────────────────────────────
FROM node:22-alpine
WORKDIR /app
COPY --from=builder /app/deploy ./
ENV NODE_ENV=production
EXPOSE 4007
CMD ["node", "dist/server.js"]
