FROM node:20-slim

ENV DEBIAN_FRONTEND=noninteractive
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright

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 so file: workspace links resolve
COPY . .

# Install and build workspace packages in dependency order
RUN cd packages/ai && npm install && npm run build
RUN cd packages/tui && npm install && npm run build
RUN cd packages/agent && npm install && npm run build
RUN cd packages/web-ui && npm install && npm run build

# Install example app dependencies (resolves file: links from monorepo)
RUN cd packages/web-ui/example && npm install

# Install Playwright Chromium
RUN cd packages/web-ui/example && 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"]
