FROM python:3.11.9-alpine AS compile-image

WORKDIR /app

COPY requirements.txt /app/

RUN sed -i 's|dl-cdn.alpinelinux.org|mirrors.ustc.edu.cn|g' /etc/apk/repositories \
    && apk add --no-cache --virtual .build-deps gcc musl-dev libffi-dev openssl-dev \
    && pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt \
    && apk del .build-deps

# ─────────────────────────────────────────────
FROM python:3.11.9-alpine AS runtime-image

WORKDIR /app

# 设置时区为东八区（北京时间），让日志和数据库 download_time 字段使用本地时间
RUN sed -i 's|dl-cdn.alpinelinux.org|mirrors.ustc.edu.cn|g' /etc/apk/repositories \
    && apk add --no-cache tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone

# 从上面自己编译的阶段复制依赖（支持任意架构）
COPY --from=compile-image /usr/local/lib/python3.11/site-packages \
     /usr/local/lib/python3.11/site-packages

# 复制应用代码（不打包 config.yaml，运行时由 entrypoint 生成）
COPY setup.py media_downloader.py do_login.py /app/
COPY module /app/module
COPY utils /app/utils
COPY entrypoint.sh /app/entrypoint.sh

RUN chmod +x /app/entrypoint.sh \
    && mkdir -p /app/downloads /app/appdata

EXPOSE 5000

ENTRYPOINT ["/bin/sh", "/app/entrypoint.sh"]
