# Vibn IDE — compiled from source (matches local dev build)
# Uses E2_HIGHCPU_32 in Cloud Build for the webpack step

FROM node:22-bullseye AS builder

RUN apt-get update && apt-get install -y libxkbfile-dev libsecret-1-dev python3 make g++ && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy dependency manifests first for better caching
COPY package.json package-lock.json lerna.json ./
COPY scripts/ scripts/
COPY configs/ configs/
COPY dev-packages/ dev-packages/
COPY packages/ packages/
COPY examples/ examples/

# Install all dependencies
# electron is needed for type declarations during compilation (pulled by examples/electron
# which is excluded by .dockerignore, so we add it manually)
RUN npm install --legacy-peer-deps && npm install --no-save electron@38.4.0

# Compile TypeScript
RUN npm run compile

# Build the browser webpack bundle (needs ~4GB RAM — Cloud Build E2_HIGHCPU_32 handles this)
RUN cd examples/browser && npx theia build --mode production

# ── Runtime stage ─────────────────────────────────────────────────────────────
FROM node:22-bullseye-slim

RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*

RUN useradd -m -s /bin/bash theia
WORKDIR /home/theia/app

# Copy the built application from builder
COPY --from=builder /app/examples/browser/lib ./lib
COPY --from=builder /app/examples/browser/src-gen ./src-gen
COPY --from=builder /app/examples/browser/package.json ./
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/packages ./packages

# Copy vibn.css into the frontend served directory
COPY examples/browser/vibn.css ./lib/frontend/vibn.css

# Copy startup script
COPY startup-custom.sh /startup.sh
RUN chmod +x /startup.sh

RUN chown -R theia:theia /home/theia
USER theia

EXPOSE 3000

ENTRYPOINT ["/startup.sh"]
