# 使用官方 Python 基础镜像 (Debian 体系)
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 防止 apt-get 安装时出现交互式弹窗（比如选择时区）导致卡死
ENV DEBIAN_FRONTEND=noninteractive

# Debian 12 (Bookworm) 的 apt 源文件变成了 /etc/apt/sources.list.d/debian.sources
# 这里替换源以加速下载，并安装必要的系统依赖
# 必须安装：Xvfb(虚拟屏幕), Chromium(浏览器核心), 中文字体(防乱码)
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
    sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || true && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
    xvfb \
    xauth \
    chromium \
    chromium-driver \
    fonts-wqy-zenhei \
    tzdata \
    && rm -rf /var/lib/apt/lists/*

# 设置时区为中国上海
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 复制依赖清单并安装 Python 库
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ && \
    pip install gunicorn -i https://mirrors.aliyun.com/pypi/simple/

# 复制整个项目到容器内
COPY . .

# 暴露 Flask 服务的 5050 端口
EXPOSE 5050

# 启动脚本：使用 xvfb-run 虚拟出一个屏幕来运行 Python 程序
# --server-args="-screen 0 1920x1080x24" 设置虚拟屏幕的分辨率
CMD ["xvfb-run", "--server-args=-screen 0 1920x1080x24", "gunicorn", "-w", "4", "-b", "0.0.0.0:5050", "--timeout", "120", "web_ui.app:app"]
