# syntax=docker/dockerfile:1.7

FROM node:20-alpine AS builder
RUN corepack enable
WORKDIR /build

COPY package.json pnpm-workspace.yaml pnpm-lock.yaml tsconfig.base.json ./
COPY apps/server/package.json ./apps/server/
COPY apps/coder/package.json ./apps/coder/

RUN pnpm install --frozen-lockfile

# Build server first (coder depends on it via workspace dep for types + inference)
COPY apps/server ./apps/server
RUN pnpm -C apps/server build

COPY apps/coder ./apps/coder
RUN pnpm -C apps/coder build

RUN pnpm deploy --filter=@boocode/coder --prod --legacy /out/coder


FROM node:20-bookworm-slim AS runtime
RUN apt-get update && apt-get install -y --no-install-recommends ripgrep git openssh-client && rm -rf /var/lib/apt/lists/*
WORKDIR /app

COPY --from=builder /out/coder ./

ENV NODE_ENV=production
EXPOSE 3000

CMD ["node", "dist/index.js"]
