# ============================================================================
# FlyGuard: Bionic Obstacle Detection Core (LCT-2026, Case 05)
# Hesai Pandar128 LiDAR (128 beams, 10 Hz / 100ms)
# Universal Production Image: NVIDIA CUDA 12.x / Ada Lovelace / Graceful CPU Fallback
# ============================================================================

# ----------------------------------------------------------------------------
# Stage 1: Optional lightweight CPU-only image (build with: --target cpu)
# ----------------------------------------------------------------------------
FROM python:3.11-slim-bookworm AS cpu

ENV DEBIAN_FRONTEND=noninteractive \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONPATH="/app:/app/tools" \
    FLYGUARD_DATA="/data" \
    FLYGUARD_DEVICE="cpu"

RUN apt-get update && apt-get install -y --no-install-recommends \
    libgomp1 \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

RUN useradd -m -u 1000 -s /bin/bash flyguard && \
    mkdir -p /app /data /app/artifacts && \
    chown -R flyguard:flyguard /app /data

WORKDIR /app
COPY --chown=flyguard:flyguard requirements.txt /app/
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

COPY --chown=flyguard:flyguard . /app/
RUN chmod +x /app/docker-entrypoint.sh

USER flyguard
VOLUME ["/data", "/app/artifacts"]
ENTRYPOINT ["/app/docker-entrypoint.sh"]
CMD ["default"]


# ----------------------------------------------------------------------------
# Stage 2: Universal Production Image with NVIDIA GPU acceleration & CPU fallback
# (Default target when building with: docker build -t flyguard:latest .)
# ----------------------------------------------------------------------------
FROM nvidia/cuda:12.4.1-runtime-ubuntu22.04 AS production

ENV DEBIAN_FRONTEND=noninteractive \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONPATH="/app:/app/tools" \
    FLYGUARD_DATA="/data" \
    FLYGUARD_DEVICE="auto" \
    NVIDIA_VISIBLE_DEVICES=all \
    NVIDIA_DRIVER_CAPABILITIES=compute,utility

# Install Python 3.11, pip, and system runtime libraries (OpenMP for LightGBM/Torch)
RUN apt-get update && apt-get install -y --no-install-recommends \
    software-properties-common \
    ca-certificates \
    libgomp1 \
    curl \
    && add-apt-repository -y ppa:deadsnakes/ppa \
    && apt-get update && apt-get install -y --no-install-recommends \
    python3.11 \
    python3.11-distutils \
    && curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 \
    && ln -sf /usr/bin/python3.11 /usr/bin/python3 \
    && ln -sf /usr/bin/python3.11 /usr/bin/python \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Create non-root user for container security compliance
RUN useradd -m -u 1000 -s /bin/bash flyguard && \
    mkdir -p /app /data /app/artifacts && \
    chown -R flyguard:flyguard /app /data

WORKDIR /app

# Cache layer: install Python dependencies with PyTorch CUDA 12.1+ wheels
COPY --chown=flyguard:flyguard requirements-gpu.txt /app/
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
    pip install --no-cache-dir -r requirements-gpu.txt

# Copy source code and artifacts
COPY --chown=flyguard:flyguard . /app/
RUN chmod +x /app/docker-entrypoint.sh

USER flyguard
VOLUME ["/data", "/app/artifacts"]

# Container healthcheck: verifies Python runtime and graceful device detection
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD python3 -c "import flyguard; from flyguard.device import get_device_info; print('healthy', get_device_info())" || exit 1

ENTRYPOINT ["/app/docker-entrypoint.sh"]
CMD ["default"]
