FROM python:3.11-slim

# System deps: FFmpeg, libs za OpenCV, Node.js + unzip za Deno (yt-dlp JS challenge)
RUN apt-get update && apt-get install -y --no-install-recommends \
    ffmpeg \
    libsm6 \
    libxext6 \
    libgl1 \
    curl \
    nodejs \
    unzip \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Deno runtime za yt-dlp YouTube nsig challenge solver
RUN curl -fsSL https://deno.land/install.sh | DENO_INSTALL=/usr/local sh -s -- -y && \
    deno --version

WORKDIR /app

# Python deps
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt && \
    pip install --no-cache-dir --upgrade --pre "yt-dlp[default]"

# App code
COPY app/ ./app/
COPY scripts/ ./scripts/
COPY templates/ ./templates/
RUN mkdir -p ./static

# Data direktorij (Coolify bo prek Persistent Storage UI bind-al volume)
RUN mkdir -p /data/uploads /data/outputs /data/jobs /data/cookies

ENV DATA_DIR=/data
ENV PYTHONUNBUFFERED=1

EXPOSE 8000

# Pre-download Whisper "small" model za faster cold start (opcijsko)
# RUN python -c "from faster_whisper import WhisperModel; WhisperModel('small', device='cpu', compute_type='int8')"

HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
  CMD curl -fsS http://localhost:8000/healthz || exit 1

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
