# Stage 1: Build the React frontend
FROM node:22-alpine AS frontend-builder
WORKDIR /app/frontend

# 主前端
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ .
RUN npm run build

# 重型插件前端：每个插件独立 vite lib build，输出 dist/plugin-element.js + wc-manifest.json
# 这一段通过 sh 循环：复制源码 → npm install → build。任何一个插件失败都会让镜像构建失败，
# 用 || true 兜底过于宽松——这里选择硬失败，便于第一时间发现 build 问题。
COPY data/plugin /app/data/plugin
RUN set -e; \
    for d in /app/data/plugin/*/frontend; do \
      if [ -f "$d/package.json" ]; then \
        echo "==> Building plugin frontend: $d"; \
        cd "$d" && npm install && npm run build; \
      fi; \
    done

# Stage 2: Build the Python backend and serve
FROM python:3.13-slim
WORKDIR /app

# Install system dependencies (for building PostgreSQL drivers and other native extensions)
# nodejs/npm are needed for stdio-mode MCP servers (e.g. npx -y @modelcontextprotocol/...)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    git \
    nodejs \
    npm \
    && rm -rf /var/lib/apt/lists/*

# Install uv package manager
RUN pip install uv

# Copy dependency files
COPY pyproject.toml uv.lock ./

# Install python dependencies without the current package (speeds up layer caching)
RUN uv sync --frozen --no-install-project --no-dev

# Copy the rest of the application
COPY . .

# Copy the built frontend static assets from Stage 1
COPY --from=frontend-builder /app/frontend/dist /app/frontend/dist
# 重型插件前端 build 产物（让 /plugin-ui/<name>/ 静态挂载有内容可挂）
COPY --from=frontend-builder /app/data/plugin /app/data/plugin

# Expose FastAPI and Ray Dashboard ports
EXPOSE 8000 8265

# Start the application
CMD ["uv", "run", "python", "main.py"]
