# One image, two apps on the platform (e.g. CapRover):
#   web:   ENTRY_SCRIPT=/app/.entry/start_core_backend.sh   (default; nginx + gunicorn)
#   tasks: ENTRY_SCRIPT=/app/.entry/start_tasks_backend.sh  (Celery worker + beat; no HTTP)
#
# Kept small: every dependency installs from a ready-made wheel (psycopg2-binary bundles the
# PostgreSQL client library), so no compilers or -dev packages are installed.
FROM python:3.12-slim

ENV DEBIAN_FRONTEND=noninteractive \
    TZ=UTC \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    DJANGO_SETTINGS_MODULE={project_name}.settings.prod

# nginx fronts gunicorn and serves /static/.
RUN apt-get update && \
    apt-get install -y --no-install-recommends nginx tzdata && \
    rm -rf /var/lib/apt/lists/* && \
    rm -f /etc/nginx/sites-enabled/default /etc/nginx/conf.d/default.conf && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

WORKDIR /app

# Dependencies first, so code changes reuse this layer. --only-binary fails the build loudly if a
# package ever needs compiling, instead of silently needing a toolchain.
COPY requirements.txt .
RUN pip install --only-binary=:all: -r requirements.txt

COPY . .

# Strip Windows line endings in case the code was uploaded from a Windows checkout.
RUN sed -i 's/\r$//' .entry/*.sh config/http.conf && chmod +x .entry/*.sh && \
    cp config/http.conf /etc/nginx/sites-available/default && \
    ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

ENV ENTRY_SCRIPT="/app/.entry/start_core_backend.sh"

EXPOSE 80

ENTRYPOINT [ "sh", "-c", "exec ${ENTRY_SCRIPT}" ]
