# =============================================================================
# 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
# 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

# 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 \
    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 \
    && rm -rf /var/lib/apt/lists/* \
    && pip3 install --no-cache-dir --break-system-packages uv

# Install Node.js and npx
RUN curl -fsSL https://deb.nodesource.com/setup_22.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 local file
COPY docker_build/himalaya.x86_64-linux.tgz /tmp/himalaya.tgz
RUN tar -xzf /tmp/himalaya.tgz -C /usr/local/bin \
    && chmod +x /usr/local/bin/himalaya \
    && rm -f /tmp/himalaya.tgz

# Install fd (alternative to find)
RUN curl -fsSL https://github.com/sharkdp/fd/releases/download/v9.0.0/fd-v9.0.0-x86_64-unknown-linux-gnu.tar.gz | \
    tar -xz --strip-components=1 -C /usr/local/bin \
    && chmod +x /usr/local/bin/fd

# Install ripgrep (rg)
RUN curl -fsSL https://github.com/BurntSushi/ripgrep/releases/download/14.1.0/ripgrep-14.1.0-x86_64-unknown-linux-musl.tar.gz | \
    tar -xz --strip-components=1 -C /usr/local/bin \
    && chmod +x /usr/local/bin/rg

# Install Chromium and chromedriver for browser automation
# 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 \
    chromium-driver \
    && ln -sf /usr/bin/chromium /usr/local/bin/chrome \
    && ln -sf /usr/bin/chromedriver /usr/local/bin/chromedriver \
    && rm -rf /var/lib/apt/lists/*

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

WORKDIR /app

# Copy pre-built binary from host
COPY target/release/picobot /app/picobot

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

# Create required directories
RUN mkdir -p /app/.picobot/workspace /app/.picobot/media /app/.picobot/tmp && \
    chown -R app:app /app

USER app
ENV HOME=/app

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

ENTRYPOINT ["/app/picobot"]
CMD ["gateway"]

EXPOSE 19876

ENV RUST_LOG=info
