FROM debian:bookworm-slim AS builder

ARG NGINX_VERSION=1.30.5
ARG KALTURA_VOD_REF=26f06877b0f2a2336e59cda93a3de18d7b23a3e2

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        build-essential \
        ca-certificates \
        curl \
        libpcre2-dev \
        zlib1g-dev \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /src/nginx-vod-module \
    && curl -fsSL "https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz" -o /tmp/nginx.tar.gz \
    && curl -fsSL "https://codeload.github.com/kaltura/nginx-vod-module/tar.gz/${KALTURA_VOD_REF}" -o /tmp/nginx-vod-module.tar.gz \
    && tar -xzf /tmp/nginx.tar.gz -C /src \
    && tar -xzf /tmp/nginx-vod-module.tar.gz -C /src/nginx-vod-module --strip-components=1 \
    && rm -f /tmp/nginx.tar.gz /tmp/nginx-vod-module.tar.gz

WORKDIR /src/nginx-${NGINX_VERSION}

RUN ./configure \
        --prefix=/usr/share/nginx \
        --sbin-path=/usr/sbin/nginx \
        --conf-path=/etc/nginx/nginx.conf \
        --pid-path=/tmp/nginx.pid \
        --error-log-path=/dev/stderr \
        --http-log-path=/dev/stdout \
        --http-client-body-temp-path=/tmp/client_body \
        --http-proxy-temp-path=/tmp/proxy \
        --http-fastcgi-temp-path=/tmp/fastcgi \
        --http-uwsgi-temp-path=/tmp/uwsgi \
        --http-scgi-temp-path=/tmp/scgi \
        --with-file-aio \
        --with-threads \
        --with-http_gzip_static_module \
        --with-http_stub_status_module \
        --with-cc-opt='-O2' \
        --add-module=/src/nginx-vod-module \
    && make -j"$(nproc)" \
    && make install

FROM debian:bookworm-slim AS vod

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        gettext-base \
        libpcre2-8-0 \
        zlib1g \
    && addgroup --system nginx \
    && adduser --system --ingroup nginx --no-create-home --home /nonexistent nginx \
    && mkdir -p /etc/nginx/templates \
    && rm -rf /var/lib/apt/lists/*

COPY --from=builder /usr/sbin/nginx /usr/sbin/nginx
COPY --from=builder /etc/nginx/mime.types /etc/nginx/mime.types
COPY config/nginx.conf.template /etc/nginx/templates/nginx.conf.template
COPY docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh

RUN chmod 0755 /usr/local/bin/docker-entrypoint.sh

USER nginx

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
    CMD curl --fail --silent http://127.0.0.1:8080/healthz || exit 1

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

FROM debian:bookworm-slim AS catalog

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ffmpeg \
        python3 \
    && addgroup --system catalog \
    && adduser --system --ingroup catalog --no-create-home --home /nonexistent catalog \
    && mkdir -p /app /cache \
    && chown catalog:catalog /app /cache \
    && rm -rf /var/lib/apt/lists/*

COPY catalog/app.py /app/app.py

USER catalog

EXPOSE 8081

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD python3 -c "from urllib.request import urlopen; urlopen('http://127.0.0.1:8081/healthz', timeout=3).read()" || exit 1

ENTRYPOINT ["python3", "/app/app.py"]
