# syntax=docker/dockerfile:1

FROM python:3.10-slim

# Point Debian and pip at Chinese mirrors reachable from the CI runner.
RUN find /etc/apt -type f \( -name '*.sources' -o -name '*.list' \) \
    -exec sed -i 's|http://deb.debian.org|https://mirrors.tuna.tsinghua.edu.cn|g' {} +

ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple

# Install build tools needed for the C++ extension plus curl for healthchecks
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    g++ \
    curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy and install Python dependencies first (better layer caching)
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Copy backend source code and the C++ extension source
COPY service ./service
COPY core ./core
COPY EfficientWordCloud ./EfficientWordCloud
COPY assets ./assets
COPY start-dev.sh ./
COPY wordcloud_generate_hybrid.py ./
COPY docker-entrypoint.sh ./

# Optional design-template seeds (copied into volume on first boot)
COPY service_design_templates ./service_design_templates_seed

# Build the C++ extension in-place
RUN cd EfficientWordCloud && python setup.py build_ext --inplace

# Runtime data directories (will be mounted as volumes)
RUN mkdir -p service_workspace service_assets service_projects service_design_templates service_fonts \
    && chmod +x /app/docker-entrypoint.sh

EXPOSE 8000

ENV PYTHONPATH=/app/EfficientWordCloud

ENTRYPOINT ["/app/docker-entrypoint.sh"]
CMD ["uvicorn", "service.app:app", "--host", "0.0.0.0", "--port", "8000"]
