FROM python:3.13-slim

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    xvfb \
    x11vnc \
    websockify \
    novnc \
    curl \
    libnss3 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxrandr2 \
    libgbm1 \
    libpango-1.0-0 \
    libcairo2 \
    libasound2 \
    libxshmfence1 \
    libgtk-3-0 \
    fonts-liberation \
    && rm -rf /var/lib/apt/lists/*

# Install Node.js 22.x from nodesource.com
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && rm -rf /var/lib/apt/lists/*

# Install uv via pip
RUN pip install uv

# Disable reflinks — overlayfs doesn't support them (EAGAIN inside Docker builds)
ENV UV_LINK_MODE=copy

# Copy backend dependency files and install Python dependencies
COPY backend/pyproject.toml backend/uv.lock /app/backend/
WORKDIR /app/backend
RUN uv sync --frozen

# Install Playwright chromium
RUN uv run playwright install chromium

# Install amplifierd
RUN uv pip install amplifierd

# Copy application files
COPY backend/*.py /app/backend/
COPY bundle/ /app/bundle/
COPY frontend/dist/ /app/frontend/dist/
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Create artifacts directory
RUN mkdir -p /app/artifacts

WORKDIR /app

EXPOSE 8080 6080

ENV DISPLAY=:99
ENV AMPLIFIERD_URL=http://localhost:8410
ENV BUNDLE_NAME=research-workbench

ENTRYPOINT ["/entrypoint.sh"]
