# Red-DiscordBot for dogebot. Python and dependencies come from uv.lock.
FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim

# Downloader clones cog repos with git; some cog requirements build C extensions.
# doge_alignment draws charts with ImageMagick (Wand needs libmagickwand),
# DejaVu Sans, and Pango (in libmagickcore-...-extra) so names with emoji fall
# back to Noto Color Emoji. These are pinned to upstream versions; a trailing
# * still takes Debian's security rebuilds (deb12uN), which an exact pin would
# break on as soon as the mirrors drop the old build. Bump them on purpose,
# then run doge_cogs' `mise run test-container`.
ARG IMAGEMAGICK_VERSION="8:6.9.11.60+dfsg-1.6+deb12u*"
ARG DEJAVU_VERSION="2.37-6"
ARG NOTO_EMOJI_VERSION="2.042-0+deb12u*"
# watch's YouTube fallback extracts audio with yt-dlp, which needs ffmpeg and
# a JavaScript runtime for YouTube's signature/EJS work. Deno is yt-dlp's
# default runtime.
ARG FFMPEG_VERSION="7:5.1.*"
ARG DENO_VERSION="2.9.8"
# deb.debian.org is Fastly, which the home network reaches through a congested
# path (NMSU -> CENIC -> Fastly: ~125 ms and ~60 KB/s, 2026-11-10) while other
# mirrors give 3-7 MB/s. Leaseweb has no debian-security; OCF Berkeley does.
# Pass --build-arg DEBIAN_MIRROR=http://deb.debian.org/debian (and the
# security one) to build elsewhere with the defaults.
ARG DEBIAN_MIRROR="http://mirror.us.leaseweb.net/debian"
ARG DEBIAN_SECURITY_MIRROR="http://mirrors.ocf.berkeley.edu/debian-security"
RUN sed -i \
      -e "s#http://deb.debian.org/debian-security#${DEBIAN_SECURITY_MIRROR}#" \
      -e "s#http://deb.debian.org/debian\$#${DEBIAN_MIRROR}#" \
      /etc/apt/sources.list.d/debian.sources \
 && grep -q "${DEBIAN_MIRROR}" /etc/apt/sources.list.d/debian.sources \
 && apt-get update \
 && apt-get install -y --no-install-recommends git build-essential \
      "libmagickwand-6.q16-6=${IMAGEMAGICK_VERSION}" \
      "libmagickcore-6.q16-6-extra=${IMAGEMAGICK_VERSION}" \
      "fonts-dejavu-core=${DEJAVU_VERSION}" \
      "fonts-noto-color-emoji=${NOTO_EMOJI_VERSION}" \
      "ffmpeg=${FFMPEG_VERSION}" \
 && rm -rf /var/lib/apt/lists/*

ENV UV_COMPILE_BYTECODE=1 \
    UV_LINK_MODE=copy \
    UV_PYTHON_DOWNLOADS=never \
    PYTHONUNBUFFERED=1

WORKDIR /app
COPY pyproject.toml uv.lock .python-version README.md ./
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev --no-install-project
COPY src ./src
COPY cogs ./cogs
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev

# watch's YouTube fallback runs this from the venv (on PATH). yt-dlp breaks
# when YouTube changes, so rebuild the image to pick up a current release.
RUN --mount=type=cache,target=/root/.cache/uv \
    uv pip install --python /app/.venv/bin/python yt-dlp

# The image has no curl/unzip; Python fetches and unpacks the pinned binary.
RUN python3 -c "import urllib.request; urllib.request.urlretrieve(\
      'https://github.com/denoland/deno/releases/download/v${DENO_VERSION}/deno-x86_64-unknown-linux-gnu.zip', \
      '/tmp/deno.zip')" \
 && python3 -c "import zipfile; zipfile.ZipFile('/tmp/deno.zip').extract('deno', '/usr/local/bin')" \
 && chmod 755 /usr/local/bin/deno \
 && rm /tmp/deno.zip \
 && deno --version

# Import dogebot.logfilter at interpreter startup, before Red sets up logging
RUN echo "import dogebot.logfilter" \
    > /app/.venv/lib/python3.11/site-packages/zz-dogebot-logfilter.pth

# Red's Downloader installs cog requirements with `pip install -U -t <lib>`,
# which can't see this venv and so picks the newest versions. Anything Red
# already imported (typing_extensions, attrs, aiohttp, ...) still comes from
# the venv, so a newer copy in lib is shadowed. Cogs then run against versions
# they weren't installed for: anyio 4.16 needs typing_extensions 4.16, but Red
# pins 4.13.2, and `d!watch` died with "cannot import name 'sentinel'". pip
# honours PIP_CONSTRAINT, so cog requirements resolve against the venv's exact
# versions. A cog that truly needs a newer one fails to install, loudly.
RUN uv pip freeze --exclude-editable > /app/constraints.txt \
 && grep -q '^typing-extensions==' /app/constraints.txt

ENV PATH=/app/.venv/bin:$PATH \
    PIP_CONSTRAINT=/app/constraints.txt \
    RED_DATA_PATH=/data
VOLUME /data
ENTRYPOINT ["dogebot-start"]
