# EPUB3 -> PDF converter (Flask + WeasyPrint) — Coolify / any Docker host
# WeasyPrint needs pango + fontconfig system libraries; CJK fonts for full-text support.
FROM python:3.12-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PORT=8030 \
    DATA_DIR=/app/data \
    PIP_NO_CACHE_DIR=1

RUN apt-get update && apt-get install -y --no-install-recommends \
    libpango-1.0-0 \
    libpangoft2-1.0-0 \
    libharfbuzz0b \
    libffi8 \
    shared-mime-info \
    fontconfig \
    fonts-dejavu-core \
    fonts-noto-cjk \
    && rm -rf /var/lib/apt/lists/* && fc-cache -f

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY app.py .
COPY templates/ ./templates/

# Persistent data (uploads/output/work) — mount a volume at /app/data
VOLUME ["/app/data"]

EXPOSE 8030

# Single worker: the in-memory JOBS registry must live in one process.
# Threads give concurrency while conversions run in their own worker threads.
CMD ["sh", "-c", "gunicorn app:app --bind 0.0.0.0:${PORT} --workers 1 --threads 8 --timeout 1200 --graceful-timeout 30 --access-logfile - --error-logfile -"]

# Health probe: pure-python urllib (slim image has no curl/wget).
# Coolify detects this and skips its own curl/wget-based check. Kept as the final line
# because Coolify's parser appends the following line to the HEALTHCHECK command.
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8030/health', timeout=5).status==200 else 1)"
