FROM python:3.12-slim

WORKDIR /app

RUN apt-get update && apt-get install -y ffmpeg gosu && rm -rf /var/lib/apt/lists/*

COPY backend/requirements.txt ./backend/requirements.txt
# yt-dlp[default] includes the EJS challenge solver scripts required by YouTube.
# nodeenv installs Node.js (v22+ required); --js-runtimes node tells yt-dlp to use it.
RUN pip install --no-cache-dir -r backend/requirements.txt && \
    pip install --no-cache-dir -U "yt-dlp[default]" && \
    pip install --no-cache-dir nodeenv && \
    nodeenv --prebuilt /opt/node && \
    ln -sf /opt/node/bin/node /usr/local/bin/node

RUN printf -- '--cache-dir /data/yt-dlp-cache\n--js-runtimes node\n' > /etc/yt-dlp.conf

COPY backend/ ./backend/

EXPOSE 8000

ENTRYPOINT ["/app/backend/entrypoint.sh"]
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000"]
