# Multi-stage build for RAG API
FROM python:3.11-slim as builder

WORKDIR /app

# Install build dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    git \
    && rm -rf /var/lib/apt/lists/*

# Copy and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt

# Runtime stage
FROM python:3.11-slim

WORKDIR /app

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Copy Python packages from builder
COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH

# Copy application code
COPY . .

# Create data directories
RUN mkdir -p /app/data/uploads /app/data/vector_store

# Expose API port
EXPOSE 8000

# Environment variables with defaults
ENV RAG_HOST=0.0.0.0
ENV RAG_PORT=8000
ENV RAG_DATA_DIR=/app/data
ENV RAG_AUTH_ENABLED=true
ENV RAG_LLM_PROVIDER=openrouter
ENV RAG_LLM_MODEL=meta-llama/llama-3.2-3b-instruct:free

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

# Run the API
CMD ["python", "-m", "uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]
