# 多账号学习控制台 —— 运行镜像
# 构建:   docker build -t study-tool:latest .
# 瘦身:   docker build --build-arg WITH_PLAYWRIGHT=0 -t study-tool:latest .   （不要直链视频播放）
FROM python:3.12-slim-bookworm

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    DEBIAN_FRONTEND=noninteractive \
    TZ=Asia/Shanghai \
    HOST=0.0.0.0 \
    PORT=8848 \
    DATA_DIR=/app/data \
    DB_PATH=/app/data/study.db \
    PLAYWRIGHT_BROWSERS_PATH=/ms-playwright

WORKDIR /app

# nodejs: script/desenc.js 要用（登录表单 rsa 字段走站点原版 DES 算法）
# curl:   健康检查用
RUN apt-get update \
 && apt-get install -y --no-install-recommends nodejs ca-certificates curl tzdata \
 && rm -rf /var/lib/apt/lists/*

# 国内镜像：默认走阿里云；要回官方源加 --build-arg PIP_INDEX_URL=https://pypi.org/simple
ARG PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple

COPY requirements.txt ./
RUN pip install -r requirements.txt -i "$PIP_INDEX_URL"

# 直链视频（fileType=other）必须用真浏览器播到结尾；不需要就 WITH_PLAYWRIGHT=0
ARG WITH_PLAYWRIGHT=1
RUN if [ "$WITH_PLAYWRIGHT" = "1" ]; then \
        playwright install --with-deps chromium && rm -rf /var/lib/apt/lists/* ; \
    fi

COPY . .
RUN mkdir -p /app/data

EXPOSE 8848

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  CMD curl -fsS "http://127.0.0.1:${PORT}/api/health" || exit 1

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