FROM alpine:latest AS artifacts

RUN apk add --no-cache curl xz
WORKDIR /tmp

RUN mkdir -p /artifacts
RUN curl -L -o typst.tar.xz "https://github.com/typst/typst/releases/download/v0.14.2/typst-x86_64-unknown-linux-musl.tar.xz" \
    && tar -xJf typst.tar.xz \
    && mv typst-x86_64-unknown-linux-musl/typst /artifacts

RUN chmod -R +x /artifacts/


FROM python:3.13-slim AS base

ENV VIRTUAL_ENV=/app/.venv \
    PATH="/app/.venv/bin:$PATH" \
    PLAYWRIGHT_BROWSERS_PATH=/usr/lib/pw-browsers

# 安装所有都需要的底层依赖
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    libfontconfig1 libgl1 libegl1 libglvnd0 mesa-vulkan-drivers at-spi2-common fontconfig \
    libasound2-data libavahi-client3 libavahi-common-data libavahi-common3 libdatrie1 \
    libfontenc1 libfribidi0 libgraphite2-3 libharfbuzz0b libice6 libpixman-1-0 \
    libsm6 libthai-data libthai0 libunwind8 libxaw7 libxcb-render0 libxfont2 libxi6 \
    libxkbfile1 libxmu6 libxpm4 libxrender1 libxt6t64 x11-common x11-xkb-utils \
    xfonts-encodings xfonts-utils xkb-data xserver-common libnspr4 libatk1.0-0t64 \
    libatk-bridge2.0-0t64 libatspi2.0-0t64 libxcomposite1 libxdamage1 libxfixes3 \
    libxkbcommon0 libasound2t64 libnss3 fonts-noto-cjk fonts-noto-cjk-extra \
    fonts-noto-color-emoji \
    && rm -rf /var/lib/apt/lists/*


FROM base AS builder

# 安装构建依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential cmake git \
    && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir uv

WORKDIR /app

COPY pyproject.toml poetry.lock ./
RUN uv sync --no-install-project



FROM base AS runtime

COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
COPY --from=artifacts /artifacts/ /usr/local/bin/

WORKDIR /app

RUN python -m playwright install chromium

COPY bot.py pyproject.toml .env.prod .env.test ./
COPY assets ./assets
COPY scripts ./scripts
COPY konabot ./konabot
COPY tests ./tests

ENV PYTHONPATH=/app

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