# Use a lightweight official Python base image
FROM python:3.10-slim

# Set system environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

# Set workspace directory
WORKDIR /app

# Install system utility for container health checks
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    && rm -rf /var/lib/apt/lists/*

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

# Copy RAG engine source code and pre-computed database
COPY app.py search_kb.py embed_docs.py .
COPY knowledge_base.db .

# Configure default environment variables
# Note: 'host.docker.internal' makes it easy to communicate with Ollama running on the host machine
ENV OLLAMA_URL=http://host.docker.internal:11434/api/embed
ENV OLLAMA_GENERATE_URL=http://host.docker.internal:11434/api/generate
ENV EMBEDDING_MODEL=qwen3-embedding:0.6b
ENV DB_PATH=/app/knowledge_base.db

# Expose port for the FastAPI REST API
EXPOSE 8000

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

# Start the RAG Engine FastAPI server by default
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
