FROM nvidia/cuda:11.8.0-runtime-ubuntu22.04

WORKDIR /app

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
	python3 \
	python3-pip \
	build-essential \
	git \
	ca-certificates \
	&& rm -rf /var/lib/apt/lists/* \
	&& ln -sf /usr/bin/python3 /usr/bin/python \
	&& ln -sf /usr/bin/pip3 /usr/bin/pip

COPY requirements/base.txt requirements/base.txt

RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
	apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
	python3-dev \
	libffi-dev \
	libssl-dev \
	cmake \
	pkg-config \
	&& rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir -r requirements/base.txt && \
	pip install --no-cache-dir mcp gpt4all sentence-transformers chromadb

RUN if [ ! -e /usr/lib/x86_64-linux-gnu/libcudart.so.11.0 ]; then \
	found=$(ls /usr/local/cuda/lib64/libcudart.so* 2>/dev/null | head -n1 || true); \
	if [ -n "$found" ]; then \
	mkdir -p /usr/lib/x86_64-linux-gnu || true; \
	ln -sf "$found" /usr/lib/x86_64-linux-gnu/libcudart.so.11.0 || true; \
	fi; \
	fi

COPY apps /app/apps
COPY config /app/config
COPY mcp_agent /app/mcp_agent
COPY manage.py /app/

ENV PYTHONUNBUFFERED=1
ENV DJANGO_SETTINGS_MODULE=config.settings
EXPOSE 8001

CMD ["python", "-m", "mcp_agent.mcp_server"]
