FROM node:22.14.0-bookworm-slim AS frontend
WORKDIR /frontend
COPY raggr-frontend/package.json raggr-frontend/yarn.lock ./
RUN yarn install --frozen-lockfile
# Copy build inputs explicitly: there is no root .dockerignore, and copying the
# whole directory would overwrite locked Linux deps with host node_modules.
COPY raggr-frontend/src ./src
COPY raggr-frontend/public ./public
COPY raggr-frontend/rsbuild.config.ts raggr-frontend/tsconfig.json raggr-frontend/postcss.config.mjs ./
RUN yarn build

FROM ghcr.io/astral-sh/uv:0.7.2 AS uv

FROM python:3.13.2-slim-bookworm

WORKDIR /app

# Node is required by the optional stdio MCP and Obsidian tools at runtime.
COPY --from=frontend /usr/local/bin/node /usr/local/bin/node
COPY --from=frontend /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm
COPY --from=uv /uv /uvx /usr/local/bin/
RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    && npm install -g obsidian-headless@0.0.8 @googleworkspace/cli@0.8.0 \
    && rm -rf /var/lib/apt/lists/*

# Add uv to PATH
ENV PATH="/root/.local/bin:$PATH"

# Homebox MCP server (stdio), pinned so the tool surface can't drift
RUN uv tool install homebox-mcp==1.1.0

# BookStack MCP server (stdio), pinned for the same reason
RUN npm install -g bookstack-mcp@5.6.0

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

# Install Python dependencies using uv
# Export the frozen graph and install it system-wide: python and aerich remain
# available to startup.sh and docker compose exec without activating a venv.
RUN uv lock --check \
    && uv export --frozen --no-dev --no-emit-project --quiet -o /tmp/requirements.txt \
    && uv pip install --system --require-hashes -r /tmp/requirements.txt \
    && rm /tmp/requirements.txt

# Copy application code
COPY *.py ./
COPY blueprints ./blueprints
COPY migrations ./migrations
COPY utils ./utils
COPY config ./config
COPY scripts ./scripts
COPY startup.sh ./
RUN chmod +x startup.sh

COPY --from=frontend /frontend/dist ./raggr-frontend/dist

# Create database directory
RUN mkdir -p /app/database /app/checkpoints

# Expose port
EXPOSE 8080

# Set environment variables
ENV PYTHONPATH=/app

# Run the startup script
CMD ["./startup.sh"]
