FROM python:3.11-slim

# Build argument: 'cpu' for Mac/CPU-only environments, 'gpu' for CUDA/NVIDIA GPU support
ARG DEVICE=gpu

# Install system dependencies needed for OpenCV, PyTorch, and Ultralytics
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libgl1 \
    libglib2.0-0 \
    libgomp1 \
    git \
    && rm -rf /var/lib/apt/lists/*

# Install uv for fast dependency resolution using pip (avoids ghcr.io network issues)
RUN pip install --no-cache-dir uv

# Set working directory
WORKDIR /workspace

# Copy dependency definition
COPY pyproject.toml ./

# Install dependencies using uv pip in system python to bypass uv.lock file hashes
# and fetch the correct PyTorch package based on the target DEVICE (CPU or GPU)
RUN --mount=type=cache,target=/root/.cache/uv \
    if [ "$DEVICE" = "cpu" ]; then \
        echo "Installing CPU-only PyTorch..." && \
        uv pip install --system --extra-index-url https://download.pytorch.org/whl/cpu -r pyproject.toml; \
    else \
        echo "Installing GPU (CUDA) PyTorch..." && \
        uv pip install --system -r pyproject.toml; \
    fi

# Copy source code and files
COPY src ./src
COPY README.md ./

# Install the project itself without re-installing dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
    uv pip install --system --no-deps -e .

# Expose Web UI port and MLflow port
EXPOSE 8000
EXPOSE 5000

# Start Web UI using the system entry point
CMD ["yolo-train-webui", "--host", "0.0.0.0", "--port", "8000"]
