# ============================================
# STAGE 1: Builder - Install packages with all the bloat
# ============================================
FROM continuumio/miniconda3:latest AS builder

# Accept build arguments
ARG GIT_VERSION=0.13.0
ARG INSTALL_CUDA=false
ARG PIP_EXTRA_INDEX_URL=""

# Set working directory
WORKDIR /app

# Set environment variables
ENV PYTHONPATH=/app
ENV KMP_DUPLICATE_LIB_OK=TRUE
ENV PYTHONUNBUFFERED=1

# Update system packages and install necessary tools
RUN apt-get update && apt-get install -y \
    build-essential \
    gcc \
    g++ \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Copy both environment files
COPY environment.yml environment-gpu.yml ./

# Create conda environment using appropriate file based on INSTALL_CUDA
RUN if [ "$INSTALL_CUDA" = "true" ]; then \
        echo "Creating GPU environment..."; \
        conda env create -n cyborgdb-service -f environment-gpu.yml; \
    else \
        echo "Creating CPU environment..."; \
        conda env create -n cyborgdb-service -f environment.yml; \
    fi && \
    conda clean -afy

# Make RUN commands use the new environment
SHELL ["conda", "run", "-n", "cyborgdb-service", "/bin/bash", "-c"]

# Copy the entire project
COPY . .

# Set version for setuptools-scm using the build arg
ENV SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CYBORGDB_SERVICE=${GIT_VERSION}

# Install the application package with aggressive cleanup
RUN if [ -n "$PIP_EXTRA_INDEX_URL" ]; then \
        echo "Using PIP_EXTRA_INDEX_URL for pip install"; \
        export PIP_EXTRA_INDEX_URL="$PIP_EXTRA_INDEX_URL"; \
    fi && \
    if [ "$INSTALL_CUDA" = "true" ]; then \
        echo "Installing with CUDA support..."; \
        pip install --no-cache-dir --no-compile .[cuda]; \
    else \
        echo "Installing CPU-only version..."; \
        pip install --no-cache-dir --no-compile .; \
    fi && \
    echo "=== Aggressive cleanup of build artifacts ===" && \
    rm -rf /root/.cache/pip/* && \
    rm -rf /home/*/.cache/pip/* && \
    rm -rf /tmp/* && \
    conda clean -afy && \
    find /opt/conda/envs/cyborgdb-service -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
    find /opt/conda/envs/cyborgdb-service -type f -name "*.pyc" -delete && \
    find /opt/conda/envs/cyborgdb-service -type f -name "*.pyo" -delete && \
    find /opt/conda/envs/cyborgdb-service -type f -name "*.a" -delete && \
    find /opt/conda/envs/cyborgdb-service -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true && \
    find /opt/conda/envs/cyborgdb-service -type d -name "test" -exec rm -rf {} + 2>/dev/null || true && \
    echo "Checking installed scripts:" && \
    ls -la /opt/conda/envs/cyborgdb-service/bin/ | grep cyborg || echo "No cyborgdb scripts found"

# ============================================
# STAGE 2: Runtime - Clean slate with only what we need
# ============================================
FROM continuumio/miniconda3:latest

# Accept build arguments (needed for labels and env vars)
ARG GIT_VERSION=0.13.0
ARG INSTALL_CUDA=false

# Set working directory
WORKDIR /app

# Set environment variables
ENV PYTHONPATH=/app
ENV KMP_DUPLICATE_LIB_OK=TRUE
ENV PYTHONUNBUFFERED=1
ENV SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CYBORGDB_SERVICE=${GIT_VERSION}

# Install only runtime dependencies (no build tools needed!)
RUN apt-get update && apt-get install -y \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Copy the entire conda environment from builder (without the build bloat)
COPY --from=builder /opt/conda/envs/cyborgdb-service /opt/conda/envs/cyborgdb-service

# Copy application files
COPY --from=builder /app/entrypoint.sh /app/entrypoint.sh
COPY --from=builder /app/cyborgdb_service /app/cyborgdb_service

# Set up entrypoint script
RUN chmod +x /app/entrypoint.sh

# Create a non-root user for security
RUN useradd -m -u 1000 cyborguser && chown -R cyborguser:cyborguser /app
USER cyborguser

# Make RUN commands use the conda environment
SHELL ["conda", "run", "-n", "cyborgdb-service", "/bin/bash", "-c"]

# Debug: Check Python version and environment
RUN echo "=== Python Version Check ===" && \
    python --version && \
    echo "=== Environment Info ===" && \
    conda info --envs && \
    if [ "$INSTALL_CUDA" = "true" ]; then \
        echo "=== PyTorch Info ===" && \
        python -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}'); print(f'CUDA version: {torch.version.cuda if torch.cuda.is_available() else \"N/A\"}')"; \
    fi

# Expose the port
EXPOSE 8000

# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8000/v1/health || exit 1

# Set default environment variables (will require override at runtime)
ENV CYBORGDB_API_KEY=""
ENV CYBORGDB_DB_TYPE=""
ENV CYBORGDB_CONNECTION_STRING=""
ENV CYBORGDB_PORT="8000"

# Add labels for documentation
LABEL org.opencontainers.image.title="CyborgDB Service"
LABEL org.opencontainers.image.description="CyborgDB service with sentence transformers"
LABEL org.opencontainers.image.documentation="Set CYBORGDB_API_KEY environment variable to run"

# Run the application with helpful entrypoint
ENTRYPOINT ["/entrypoint.sh"]