# =============================================================================
# PicoBot Docker Image
# =============================================================================
# Build binary on host:
#   cargo build --release
#
# Build image:
#   docker build -t picobot .
#
# Run gateway:  docker run -d -v ~/.picobot:/app/.picobot -p 19876:19876 picobot gateway --host 0.0.0.0
# Run chat:     docker run -it -v ~/.picobot:/app/.picobot picobot chat
# =============================================================================

FROM debian:trixie-slim

LABEL org.opencontainers.image.title="PicoBot"
LABEL org.opencontainers.image.description="AI agent gateway and chat client"
LABEL org.opencontainers.image.source="https://github.com/your-repo/picobot"

# Avoid interactive prompts
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai

# Configure domestic mirrors for pip, uv, npm (China)
ENV PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
ENV UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/

# Install base tools, Python, and uv in one layer to reduce duplication
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    tzdata \
    tini \
    curl \
    gnupg \
    git \
    jq \
    tree \
    zip \
    unzip \
    sqlite3 \
    openssh-client \
    sshpass \
    dnsutils \
    poppler-utils \
    fonts-wqy-zenhei \
    fonts-wqy-microhei \
    python3 \
    python3-pip \
    python3-venv \
    && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
    && rm -rf /var/lib/apt/lists/* \
    && pip3 install --no-cache-dir --break-system-packages uv

# Install Node.js and npx. agent-browser's npm package requires Node.js 24+.
RUN curl -fsSL https://deb.nodesource.com/setup_24.x | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && npm config set registry https://registry.npmmirror.com \
    && npm cache clean --force \
    && rm -rf /var/lib/apt/lists/*

# Install himalaya (CLI email client) from the official pre-built binary release
RUN curl -sSL https://raw.githubusercontent.com/pimalaya/himalaya/master/install.sh | sh

# Install fd (alternative to find) and ripgrep from Debian. Debian names the
# fd binary `fdfind`, so expose the conventional `fd` name as well.
RUN apt-get update && apt-get install -y --no-install-recommends \
    fd-find \
    ripgrep \
    && ln -sf /usr/bin/fdfind /usr/local/bin/fd \
    && rm -rf /var/lib/apt/lists/*

# Install Chromium plus the validated native agent-browser CLI. PicoBot talks
# to agent-browser over its JSON CLI contract; ChromeDriver/WebDriver is not used.
# Debian's chromium package is real (not a snap shim like Ubuntu 24.04).
RUN apt-get update && apt-get install -y --no-install-recommends \
    chromium \
    && ln -sf /usr/bin/chromium /usr/local/bin/chrome \
    && npm install -g --registry=https://registry.npmjs.org agent-browser@0.33.0 \
    && npm cache clean --force \
    && rm -rf /var/lib/apt/lists/*

# Create non-root user
RUN useradd -m -s /bin/bash app

WORKDIR /app

# Install the pre-built binary in the standard executable path.
COPY target/release/picobot /usr/local/bin/picobot

# Copy config template
COPY resources/templates/config.example.json /app/config.json.example

# Create persistent application directories. Transient browser data stays in
# /tmp; optional Chrome profiles live under the persisted .picobot volume.
RUN mkdir -p /app/.picobot/workspace /app/.picobot/media /app/.picobot/browser/profiles && \
    chown -R app:app /app

USER app
ENV HOME=/app

# Environment variables for Chromium in containers
ENV CHROME_BIN=/usr/bin/chromium
ENV AGENT_BROWSER_EXECUTABLE_PATH=/usr/bin/chromium
ENV TMPDIR=/tmp

ENTRYPOINT ["/usr/local/bin/picobot"]
CMD ["gateway"]

EXPOSE 19876

ENV RUST_LOG=info
