FROM python:3.10-slim

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Global setting to suppress "Running as root" warning for all pip commands
ENV PIP_ROOT_USER_ACTION=ignore

# Prevent debconf errors during build
ARG DEBIAN_FRONTEND=noninteractive

# Update apt source to TUNA (Optional, speeds up system package install) and install dependencies
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources || true \
    && apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    default-libmysqlclient-dev \
    pkg-config \
    tzdata \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .

# Use Tsinghua PyPI Mirror & Upgrade pip
RUN pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

COPY . .

# Run application with Gunicorn + Uvicorn Workers (multi-process)
# This enables cross-process WebSocket message broadcasting via Redis Pub/Sub
CMD ["gunicorn", "app.main:app", "-k", "uvicorn.workers.UvicornWorker", "--workers", "4", "--bind", "0.0.0.0:8000", "--timeout", "120", "--keep-alive", "5"]
