FROM python:3.10-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1

# libgl/libglib are needed by opencv-python
RUN apt-get update && apt-get install -y --no-install-recommends \
        libgl1 libglib2.0-0 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY requirements.txt .
RUN pip install --upgrade pip && pip install -r requirements.txt

# Optional: YOLO object detection (/api/faces/analyze). Adds ~1GB.
# Build with:  docker compose build --build-arg INSTALL_ML=1
ARG INSTALL_ML=0
RUN if [ "$INSTALL_ML" = "1" ]; then \
        pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu \
        && pip install ultralytics ; \
    fi

COPY . .

RUN chmod +x deploy/entrypoint.sh \
    && mkdir -p /app/media /app/logs /app/staticfiles

EXPOSE 8000
ENTRYPOINT ["deploy/entrypoint.sh"]
# ASGI: serves the REST API and WebSockets (/ws/devices/<id>/).
# Worker count comes from WEB_CONCURRENCY (set by deploy/entrypoint.sh).
CMD ["gunicorn", "homebuddy_api.asgi:application", \
     "-k", "uvicorn.workers.UvicornWorker", \
     "--bind", "0.0.0.0:8000", "--timeout", "120", \
     "--access-logfile", "-"]
