# syntax=docker/dockerfile:1
# Base images are pinned by digest (Dependabot updates the digests), so a rebuild uses the same bytes.

# --- 1. Build the web UI --------------------------------------------------------------------
FROM node:22-slim@sha256:be979e5523c76fa4d5ca4ebd3099078f220aa47ec39c48ad5b5df9314debd6eb AS web
WORKDIR /web
COPY web/package.json web/package-lock.json ./
RUN npm ci
COPY web ./
RUN npm run build

# --- 2. Runtime: Python + ffmpeg + Chromium, with the web UI inside the package ---------------
FROM python:3.12-slim@sha256:5b5fe805d367f8f5a359ded6ab2652b7f9a206453ee6ee789c0045ba6ec15016

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PLAYWRIGHT_BROWSERS_PATH=/ms-playwright

RUN apt-get update \
 && apt-get install -y --no-install-recommends ffmpeg fonts-noto-core fonts-noto-color-emoji \
 && rm -rf /var/lib/apt/lists/*

# deno lets yt-dlp solve YouTube's JavaScript challenges (pinned official image).
COPY --from=denoland/deno:bin-2.6.0@sha256:e76562c56fbcbda664e9ca7dce0a934496401fe78c0f64d03f76b9b62b3064b8 /deno /usr/local/bin/deno

WORKDIR /app
COPY pyproject.toml README.md LICENSE ./
COPY src ./src
COPY --from=web /web/dist ./src/vicecut/_spa
RUN pip install --no-cache-dir . \
 && playwright install --with-deps chromium

RUN useradd --create-home --uid 10001 vicecut \
 && mkdir -p /workspace && chown vicecut:vicecut /workspace
USER vicecut

# config.toml, .env-provided secrets and the data/ folder live in the mounted /workspace volume.
WORKDIR /workspace
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s \
  CMD python -c "import urllib.request as u; u.urlopen('http://127.0.0.1:8080/api/v1/healthz', timeout=3)"
ENTRYPOINT ["vicecut"]
# Studio (web UI + engine controls). Listening beyond the container needs VICECUT_PASSWORD.
CMD ["serve", "--host", "0.0.0.0", "--port", "8080"]
