FROM python:3.10-slim-bookworm

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    TZ=Asia/Shanghai \
    LANG=C.UTF-8 \
    DISPLAY=:99 \
    XDG_CONFIG_HOME=/tmp/xdg_config \
    CHROME_BIN=/opt/ungoogled-chromium/chrome \
    MIHOMO_BIN=/bin/mihomo

# 安装依赖（移除 chromium / driver）
RUN apt-get update && apt-get install -y --no-install-recommends \
    gzip \
    xz-utils \
    xvfb \
    xauth \
    fonts-noto-cjk \
    fonts-wqy-zenhei \
    procps \
    dumb-init \
    libgl1 \
    libglib2.0-0 \
    dbus \
    dbus-x11 \
    libnss3 \
    libnspr4 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libasound2 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN dbus-uuidgen > /var/lib/dbus/machine-id

WORKDIR /app

# 复制并解压 chromium
COPY downloads/ungoogled-chromium-144.0.7559.132-1-x86_64_linux.tar.xz /tmp/chrome.tar.xz
RUN mkdir -p /opt/ungoogled-chromium && \
    tar -xJf /tmp/chrome.tar.xz -C /opt/ungoogled-chromium --strip-components=1 && \
    rm /tmp/chrome.tar.xz

# 复制并解压 mihomo
COPY downloads/mihomo-linux-amd64-alpha-98aa7e6.gz /tmp/mihomo.gz
RUN gzip -dc /tmp/mihomo.gz > /bin/mihomo && chmod +x /bin/mihomo && rm /tmp/mihomo.gz

# 依赖安装
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 启动脚本
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# 项目代码
COPY . .

# 临时目录
RUN mkdir -p /app/data/temp_browser_data && chmod 777 /app/data/temp_browser_data

ENTRYPOINT ["/usr/bin/dumb-init", "--", "/entrypoint.sh"]