FROM python:3.13-slim-bookworm as base

# Avoid prompts from apt
ENV DEBIAN_FRONTEND=noninteractive

# Configure mirror
ARG ENABLE_MIRROR=false
ENV PIP_INDEX_URL=${ENABLE_MIRROR:+https://mirrors.aliyun.com/pypi/simple}
RUN if [ "$ENABLE_MIRROR" = "true" ]; then \
    rm -rfv /etc/apt/sources.list.d/* && \
    echo "deb http://ftp.cn.debian.org/debian bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
    echo "deb http://ftp.cn.debian.org/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
    echo "deb http://ftp.cn.debian.org/debian bookworm-backports main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
    echo "deb http://ftp.cn.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*; \
    fi

# Install dependencies
RUN apt-get update && apt-get install -y \
    nodejs \
    npm \
    git \
    xvfb \
    x11vnc \
    openbox \
    supervisor \
    novnc \
    websockify \
    procps \
    xdg-utils \
    python3-xdg \
    x11-xserver-utils \
    curl \
    --no-install-recommends

# Install noVNC
RUN git clone --depth 1 --branch v1.6.0 https://github.com/novnc/noVNC.git /usr/local/novnc \
    && git clone --depth 1 --branch v0.13.0 https://github.com/novnc/websockify /usr/local/novnc/utils/websockify

# Set up working directory
WORKDIR /app/view_server

# Install Playwright and browsers with dependencies
RUN npm install playwright@1.54
RUN npx playwright install chromium --with-deps

# Set up supervisord configuration
COPY docker/resources/supervisord.conf /etc/supervisor/supervisord.conf

# Copy scripts
COPY docker/resources/start.sh start.sh
COPY docker/resources/playwright-server.js playwright-server.js
COPY docker/resources/x11-setup.sh x11-setup.sh

# Make scripts executable
RUN chmod +x start.sh x11-setup.sh

# Create a simple openbox configuration to only show the browser window
RUN mkdir -p /root/.config/openbox
COPY docker/resources/openbox-rc.xml /root/.config/openbox/rc.xml

ENV PLAYWRIGHT_WS_PATH="default"
ENV PLAYWRIGHT_PORT=37367
ENV NO_VNC_PORT=5901

# Set the display environment variable
ENV DISPLAY=:99

COPY docker/resources/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

# MCP Server Container
FROM base

RUN pip install -U --break-system-packages uv

COPY mcp_server_proxy /app/mcp_server_proxy

RUN cd /app/mcp_server_proxy && uv sync --python-preference=only-system

RUN apt-get install -y --no-install-recommends \
    wget \
    unzip \
    libterm-readline-perl-perl \
    libmupdf-dev \
    vim \
    libmagic1

WORKDIR /root/workspace

EXPOSE 4242

HEALTHCHECK --interval=10s --timeout=10s --start-period=10s --retries=12 \
    CMD curl -f http://localhost:4242/health || exit 1

CMD ["sh", "-c", "/app/view_server/start.sh && cd /app/mcp_server_proxy && uv run --no-sync -m mcp_server_proxy.main"]
