FROM node:20-slim

ENV DEBIAN_FRONTEND=noninteractive
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
ENV HUSKY=0

RUN apt-get update && apt-get install -y --no-install-recommends \
    git curl ca-certificates \
    libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 \
    libdrm2 libdbus-1-3 libxkbcommon0 libatspi2.0-0 \
    libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libpango-1.0-0 \
    libasound2 libwayland-client0 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /monorepo

# Copy entire monorepo
COPY . .

# Remove husky prepare script from root package.json
RUN node -e "const p=require('./package.json'); delete p.scripts.prepare; require('fs').writeFileSync('./package.json', JSON.stringify(p,null,2))"

# Install ALL workspace deps from root — this hoists tsgo and other bins
RUN npm install

# Build packages in dependency order
RUN cd packages/ai && npm run build
RUN cd packages/tui && npm run build
RUN cd packages/agent && npm run build
RUN cd packages/web-ui && npm run build

# Install Playwright Chromium
RUN npx playwright install chromium

WORKDIR /monorepo/packages/web-ui/example

EXPOSE 5173 7700

ENV TOOL_SERVER_PORT=7700

CMD ["sh", "-c", "node server/tool-server.mjs & npx vite --host 0.0.0.0 --port 5173"]
