# FlowFabric API. Built from the repository root (it needs db/ for the schema
# and migrations):  docker build -f backend/Dockerfile .
FROM python:3.14-slim

# tesseract: Page Designer AI Assist's OCR step (app/core/ocr.py).
# nodejs: Node script tasks (app/crud/code_runner.py).
RUN apt-get update \
 && apt-get install -y --no-install-recommends tesseract-ocr nodejs libglib2.0-0t64 \
 && rm -rf /var/lib/apt/lists/*

ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PIP_DISABLE_PIP_VERSION_CHECK=1
# Refuses to start with the dev-only secrets (app/core/config.py).
ENV ENVIRONMENT=production LOG_FORMAT=json

WORKDIR /app/backend
COPY backend/requirements.txt .
RUN pip install -r requirements.txt

COPY db /app/db
COPY backend/app ./app
COPY backend/scripts ./scripts
COPY backend/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

RUN useradd --system --uid 10001 --home-dir /app flowfabric \
 && mkdir -p uploads && chown flowfabric uploads \
 && chmod +x /usr/local/bin/docker-entrypoint.sh
USER flowfabric

EXPOSE 8000
HEALTHCHECK --interval=15s --timeout=5s --start-period=30s \
  CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/ready', timeout=4)"
ENTRYPOINT ["docker-entrypoint.sh"]
# One worker: the scheduler and storage builds run in-process (app/main.py).
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers", "--forwarded-allow-ips", "*"]
