# ---------- 构建阶段 ----------
FROM python:3.11-slim AS builder

WORKDIR /app

# 安装构建依赖（matplotlib 需要）
RUN apt-get update && \
    apt-get install -y --no-install-recommends gcc && \
    rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt

# ---------- 运行阶段 ----------
FROM python:3.11-slim

LABEL maintainer="xhs-autobot"
LABEL description="小红书 AI 爆文生产工坊"

WORKDIR /app

# matplotlib 中文字体 + 运行时依赖
RUN apt-get update && \
    apt-get install -y --no-install-recommends fonts-noto-cjk curl && \
    rm -rf /var/lib/apt/lists/*

# 从构建阶段复制 Python 包
COPY --from=builder /install /usr/local

# 复制项目代码
COPY config_manager.py llm_service.py sd_service.py mcp_client.py main.py ./
COPY requirements.txt ./
COPY config.example.json ./

# 创建工作目录
RUN mkdir -p xhs_workspace

# Gradio 默认端口
EXPOSE 7860

# 环境变量
ENV GRADIO_SERVER_NAME="0.0.0.0"
ENV GRADIO_SERVER_PORT="7860"
ENV PYTHONUNBUFFERED=1
ENV MPLCONFIGDIR=/tmp/matplotlib

HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
    CMD curl -f http://localhost:7860/ || exit 1

CMD ["python", "main.py"]
