FROM node:22-bookworm-slim

WORKDIR /app

ENV NODE_ENV=production
ENV HOSTNAME=0.0.0.0
ENV PORT=3000
ENV CAD_AGENT_STUDIO_ROOT=/app/cad-agent-studio
ENV CAD_ENGINE_ROOT=/app/text-to-cad
ENV CAD_AGENT_STUDIO_CONFIG=/app/cad-agent-studio/config/llm.config.yaml
ENV PYTHONPATH=/app/text-to-cad/packages/cadpy/src

RUN sed -i \
  -e 's|http://deb.debian.org/debian|http://mirrors.aliyun.com/debian|g' \
  -e 's|http://deb.debian.org/debian-security|http://mirrors.aliyun.com/debian-security|g' \
  /etc/apt/sources.list.d/debian.sources \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
    ca-certificates \
    python3 \
    python3-pip \
    python3-venv \
    libgl1 \
    libglib2.0-0 \
    libxrender1 \
    libxext6 \
    libsm6 \
    libice6 \
    libfontconfig1 \
    fontconfig \
    fonts-dejavu-core \
    libgomp1 \
  && rm -rf /var/lib/apt/lists/*

RUN npm config set registry https://registry.npmmirror.com

RUN python3 -m pip install \
  --break-system-packages \
  --no-cache-dir \
  -i https://pypi.tuna.tsinghua.edu.cn/simple \
  build123d

COPY text-to-cad ./text-to-cad
COPY cad-agent-studio ./cad-agent-studio

RUN cd cad-agent-studio && npm install --include=dev --no-audit --no-fund
RUN cd cad-agent-studio && npm run build

EXPOSE 3000

CMD ["sh", "-lc", "cd /app/cad-agent-studio && npm run start -- -H 0.0.0.0 -p ${PORT}"]
