# ==============================================================================
# Stage 1: builder
# Installs the Python toolchain + compilers, creates the virtual environment and
# builds the legacy (CPU-baseline=none) NumPy wheel. None of these compilers end
# up in the final image — only the artifacts below are copied forward.
# ==============================================================================
FROM lscr.io/linuxserver/ffmpeg:version-8.1.2-cli AS builder

ENV DEBIAN_FRONTEND=noninteractive
ENV UV_PROJECT_ENVIRONMENT=/dispatcharrpy
ENV VIRTUAL_ENV=/dispatcharrpy
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy

# --- Install Python 3.13 and build dependencies ---
# Note: Hardware acceleration (VA-API, VDPAU, NVENC) already included in base ffmpeg image
RUN apt-get update && apt-get install --no-install-recommends -y \
    ca-certificates software-properties-common gnupg2 curl wget \
    && add-apt-repository ppa:deadsnakes/ppa \
    && apt-get update \
    && apt-get install --no-install-recommends -y \
    python3.13 python3.13-dev python3.13-venv libpython3.13 \
    libpcre2-dev \
    build-essential gcc g++ gfortran libopenblas-dev libopenblas0 ninja-build \
    && rm -rf /var/lib/apt/lists/*

# --- Install UV ---
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

# --- Create Python virtual environment and install dependencies ---
WORKDIR /tmp/build
COPY pyproject.toml /tmp/build/
COPY version.py /tmp/build/
COPY README.md /tmp/build/
RUN uv sync --python 3.13 --no-cache --no-install-project --no-dev
WORKDIR /

# --- Build legacy NumPy wheel for old hardware (store for runtime switching) ---
# build/pip are installed into the venv only long enough to produce the wheel,
# then uninstalled so they are not carried into the final image's venv.
RUN uv pip install --python $UV_PROJECT_ENVIRONMENT/bin/python --no-cache build pip && \
    cd /tmp && \
    $UV_PROJECT_ENVIRONMENT/bin/python -m pip download --no-binary numpy --no-deps numpy && \
    tar -xzf numpy-*.tar.gz && \
    cd numpy-*/ && \
    $UV_PROJECT_ENVIRONMENT/bin/python -m build --wheel -Csetup-args=-Dcpu-baseline="none" -Csetup-args=-Dcpu-dispatch="none" && \
    mv dist/*.whl /opt/ && \
    cd / && rm -rf /tmp/numpy-* /tmp/*.tar.gz /tmp/build && \
    uv pip uninstall --python $UV_PROJECT_ENVIRONMENT/bin/python build pip

# Build Comskip from upstream master (newer than distro packages). Links against
# distro libav (same libraries the old universe package used), not linuxserver ffmpeg.
#
# Latest GitHub release is still V0.83, which does not compile here: gcc 15
# rejects void OutputFrame(); (fixed on master) and FFmpeg 8 removed
# AVCodecContext.ticks_per_frame (not in any tag yet). When they tag a
# release that builds on Ubuntu 26.04, switch the tarball to
#   https://github.com/erikkaashoek/Comskip/archive/refs/tags/${TAG}.tar.gz
# and delete the ticks_per_frame sed below if that tree already compiles.
#
# Distro libav (via libavformat-dev) is required: linuxserver ships libav
# statically inside ffmpeg, with no shared libavcodec to link comskip against.
# PKG_CONFIG_LIBDIR selects distro headers. -rpath-link is the link-time search
# path for pangocairo's fontconfig. gcc's default --enable-new-dtags turns
# -rpath into DT_RUNPATH, which is searched after LD_LIBRARY_PATH and is not
# inherited by libpangoft2. linuxserver sets LD_LIBRARY_PATH=/usr/local/lib, so
# the older /usr/local fontconfig wins and FcConfigSetDefaultSubstitute is
# missing. --disable-new-dtags writes DT_RPATH, which is searched first and
# inherited by transitive deps. ffmpeg is a separate process and is unchanged.
RUN apt-get update && apt-get install --no-install-recommends -y \
    autoconf automake libtool pkg-config \
    libargtable2-dev libavformat-dev libswscale-dev \
    && mkdir /tmp/comskip \
    && curl -fsSL https://github.com/erikkaashoek/Comskip/archive/refs/heads/master.tar.gz \
        | tar -xz --strip-components=1 -C /tmp/comskip \
    && cd /tmp/comskip \
    && sed -i \
        -e '/#include <libavcodec\/avcodec.h>/a #include <libavcodec/codec_desc.h>' \
        -e '/is->dec_ctx->ticks_per_frame = 1/d' \
        -e 's/is->dec_ctx->ticks_per_frame/(is->dec_ctx->codec_descriptor \&\& (is->dec_ctx->codec_descriptor->props \& AV_CODEC_PROP_FIELDS) ? 2 : 1)/g' \
        mpeg2dec.c \
    && MULTIARCH="$(dpkg-architecture -qDEB_HOST_MULTIARCH)" \
    && DISTRO_LIB="/usr/lib/${MULTIARCH}" \
    && export PKG_CONFIG_LIBDIR="${DISTRO_LIB}/pkgconfig" \
    && export LDFLAGS="-L${DISTRO_LIB} -Wl,-rpath-link,${DISTRO_LIB} -Wl,-rpath,${DISTRO_LIB} -Wl,--disable-new-dtags" \
    && ./autogen.sh && ./configure && make -j"$(nproc)" \
    && strip comskip \
    && install -m 0755 comskip /usr/local/bin/comskip \
    && rm -rf /tmp/comskip \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# ==============================================================================
# Stage 2: final runtime image
# Same ffmpeg base, but installs only the runtime system libraries (no compilers)
# and copies the prebuilt virtual environment + NumPy wheel from the builder.
# ==============================================================================
FROM lscr.io/linuxserver/ffmpeg:version-8.1.2-cli AS final

ENV DEBIAN_FRONTEND=noninteractive
ENV UV_PROJECT_ENVIRONMENT=/dispatcharrpy
ENV VIRTUAL_ENV=/dispatcharrpy
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy

# --- Install runtime system dependencies (no build toolchain) ---
# python3.13 + libpython3.13 back the copied venv; libpcre2-8-0 backs uWSGI;
# libopenblas0 backs the legacy NumPy wheel; libargtable2-0 is for comskip;
# ca-certificates/gnupg2/curl/wget and software-properties-common are kept
# for TLS and the AIO runtime apt step.
RUN apt-get update && apt-get install --no-install-recommends -y \
    ca-certificates software-properties-common gnupg2 curl wget \
    && add-apt-repository ppa:deadsnakes/ppa \
    && apt-get update \
    && apt-get install --no-install-recommends -y \
    python3.13 python3.13-venv libpython3.13 \
    libpcre2-8-0 libopenblas0 procps pciutils \
    nginx libargtable2-0 \
    vlc-bin vlc-plugin-base \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# --- Install UV (used at runtime to swap in the legacy NumPy wheel) ---
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

# --- Copy prebuilt virtual environment, legacy NumPy wheel, and comskip ---
COPY --from=builder /dispatcharrpy /dispatcharrpy
COPY --from=builder /opt/numpy-*.whl /opt/
COPY --from=builder /usr/local/bin/comskip /usr/local/bin/comskip

# --- Set up Redis 7.x ---
RUN curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg && \
    echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | \
    tee /etc/apt/sources.list.d/redis.list && \
    apt-get update && apt-get install -y redis-server && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# --- Set up PostgreSQL 17.x ---
RUN curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg && \
    echo "deb [signed-by=/usr/share/keyrings/postgresql-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | \
    tee /etc/apt/sources.list.d/pgdg.list && \
    apt-get update && apt-get install -y postgresql-17 postgresql-contrib-17 && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# Create render group for hardware acceleration support with GID 109
RUN groupadd -r -g 109 render || true

# /etc/localtime as a regular file, not a symlink to Etc/UTC. Bind-mounting
# the host's /etc/localtime then overlays this path instead of following the
# symlink and replacing the IANA UTC zone with the host timezone.
RUN rm -f /etc/localtime && cp -a /usr/share/zoneinfo/Etc/UTC /etc/localtime

ENTRYPOINT ["/app/docker/entrypoint.sh"]
